Re: why isn't there a placement delete syntax
Ulrich Eckhardt ha scritto:
Now, placement new only removes the task of allocating storage, the
storage
is already provided. Similarly, you only invoke the destructor lateron
instead of calling delete because the memory management is left out.
The syntax for those steps is
char buffer[sizeof (X)];
X* x = new (buffer) X;
x->~X();
This is correct, but it's a very limited view of the placement new
syntax. Placement new can have any type as parameter (not necessarily a
pointer to uninitialized memory) and can have even more than one parameter.
In fact, the OP was using "new(heap) Object" that would match the signature:
void* operator new (Heap&, size_t);
In this case memory management is not left out. Instead you are
providing extra info to the allocation function that shall be used to
allocate the memory. The typical use case is the following:
Heap a;
Heap b;
Object* fromA = new(a) Object; // allocate from heap a
Object* fromB = new(b) Object; // allocate from heap b
As the new expression actually allocate memory through the passed heap
object, simply calling the destructor with a->~Object() will cause a
memory leak as the heap don't get the opportunity to release the memory.
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]