SimonKagstrom / cibyl

A MIPS to Java bytecode binary translator
http://cibyl.org
GNU Lesser General Public License v2.1
51 stars 9 forks source link

C++'s placement new #1

Closed maxm closed 3 years ago

maxm commented 12 years ago

I'm trying to compile a project that uses std::vector from STLport with Cibyl. It fails with errors like:

error: no matching function for call to 'operator new(unsigned int, MyType&)'

It seems it can't find an implementation for the placement new operator. A simple way to reproduce the problem is:

int* p = (int*)malloc(sizeof(int));
new(p) int;

I managed to get the program working by substituting the uses of placement new with constructing somewhere else and copying, but I would like to know if there is a better solution.

SimonKagstrom commented 12 years ago

Well, one way could perhaps be to simply define these in some header file (new seems to be the name),

  void * operator new (std::size_t, void * p) { return p ; }
  void * operator new[] (std::size_t, void * p) { return p ; }
  void operator delete (void *, void *) { }
  void operator delete[] (void *, void *) { }

Cibyl has fairly rudimentary C++ support, and this is one of the things I've missed. I've also switched computer, so I'll need to get the cross-compiler built again to get the Cibyl environment working :-)

maxm commented 12 years ago

Yep, that works.

I thought it would be necessary to somehow call the object's constructor in a placement new overload. But it just works.

Thanks! Great work on Cibyl :)