Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

null_pointer

What is the proper syntax for invoking placement delete?

This topic is 5526 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
$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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!