What is the proper syntax for invoking placement delete?

Started by
33 comments, last by null_pointer 20 years, 11 months ago
Sorry for asking such a simple question, but Visual C++ .NET 2002 always seems to invoke the standard delete instead of the placement delete, and this causes debug assertions in the standard library. What is the proper syntax for invoking placement delete? I have tried the following:
  
delete (address) object;
delete (address, object);
  
The former will not compile. The Visual C++ documentation shows the latter syntax in an example program, but the compiler translates this into a call to the non-placement delete. Is this a bug in the compiler?
Advertisement
Whats wrong with

delete Object; 
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
quote:Original post by null_pointer
What is the proper syntax for invoking placement delete?

There''s no such thing as "placement delete". Call the dtor.
quote:There''s no such thing as "placement delete". Call the dtor.


Yes, there is. Find it in the standard.
$18.4.1.3 (7-10):

quote:
  void operator delete(void* ptr, void*) throw(); 


7 Effects: Intentionally performs no action.
8 Notes: Default function called when any part of the initialization in a placement new expression that
invokes the library’s nonarray
placement operator new terminates by throwing an exception (5.3.4).

  void operator delete[](void* ptr, void*) throw(); 


9 Effects: Intentionally performs no action.
10 Notes: Default function called when any part of the initialization in a placement new expression that
invokes the library’s array placement operator new terminates by throwing an exception (5.3.4).

quote:Whats wrong with delete Object;


The standard operators new and delete allocate and deallocate the memory and invoke the constuctor and destructor whereas the standard operators *placement* new and delete just invoke the constructor and destructor on memory that has already been allocated. This is useful in writing a custom memory manager, among other things.
OK I get it!

Why not just use the standard new and delete then?

new=allocate mem,

delete = dealocate mem.

If memory is ''already'' allocated, this is a bad thing, isnt it?
TechleadEnilno, the Ultima 2 projectwww.dr-code.org/enilno
quote:Original post by null_pointer
Yes, there is. Find it in the standard.

"placement delete" does not occur anywhere in the Standard.
quote:
$18.4.1.3 (7-10):

Where does that say "placement delete"?

If you want to destroy an object allocated using placement new, then you call the dtor for the object, and then perform clean-up according to whatever your custom allocation schema demands.
quote:Original post by RhoneRanger
OK I get it!

No you don''t. You''ve entirely missed the point.
quote:Original post by null_pointer
Yes, there is. Find it in the standard.


"placement delete" only gets called if the constructor called from placement new throws an exception. Otherwise, call the destructor and operator delete manually.

MSN
The problem is that C++ provides no other way to explicitly invoke a constructor. Let''s say you wanted to write your own implementation of std::vector. Without placement new and delete, how would you reserve memory without actually constructing objects in it? And then when you needed to resize the vector, how would you construct objects in the reserved memory? For these things you would need to use placement new to indirectly invoke the constructor.

This topic is closed to new replies.

Advertisement