Thanks. Almost works.
I'm having trouble getting the placement new to compile tho.
class obj { public: obj() : x(0) {} ~obj() {} int x;};int main() { obj *x = (obj *) ::operator new(sizeof(obj)); // Allocate memory new (x) obj; // placement new /* Other tries: new (x) obj; new (*x) obj; new (x) obj(); new (*x) obj(); obj *y = new (x) obj; */ x->~obj(); // Destructor call. ::operator delete(x); // Deallocate memory return 0;}
I also tried:
class obj { public:// obj() : x(0) {} obj(int y) : x(y) {} ~obj() {} int x;};int main() { void* buf = ::operator new(sizeof(obj)); // allocate only obj* f = new(buf) obj(5); // construct only, placement new f->~obj(); // destruct only, must be done for user-defined types ::operator delete(buf); // de-allocate only return 0;}
Both fail to compile on the placement new.
Compiler Error:
37 ...\Main.cpp no matching function for call to `operator new(unsigned int, void*&)'
note ...\<internal>:0 candidates are: void* operator new(unsigned int)
As for vectors, I've looked at them a bit, but I'm using custom allocators and things get a little tricky there.
If it matters, I'm using DevC++ 4.9.9.2 in Windows/x86, which uses GCC.