Sign in to follow this  
wyrzy

Placement New Question

Recommended Posts

Does anyone know if placement new requires the supplied address to be located on the heap? After googling, there seems to be conflicting opinions, one here and one here. The C++ FAQ Lite (which I have found to be a reliable resource) says that you can use the following code: ---------
char place[sizeof(Fred)];     
Fred* f = new(place) Fred(); 
--------- However, DevX (which also seems to be reliable) states the following is undefined: ---------
char place[1024];  //bad idea: stack allocation instead of heap
Person *p = new ( place ) Person;  //undefined behavior
--------- and the below should be used instead: ---------
char *pbuff = new char[1024]; // heap allocation using plain new
Person *p = new (pbuff) Person; // placement new uses a pre-allocated buffer
--------- Which appears to contradict what the C++ FAQ Lite is saying. I have also seem some books use the automatic allocation method when discussing placement new, as opposed to allocating a buffer with malloc or new. Does anyone know which way is standards compliant? Both work with MSVC 7.1, but I don't want my code to be tied down to a specific compiler, or (worse) break when MSVC 2005 releases. Is the automatic allocation way undefined by the standard?

Share this post


Link to post
Share on other sites
The problem isn't heap/stack allocation but memory alignment. You can't precisly know the alignment of stackbased objects but the block returned from malloc/new is guaranteed to adhere to the most strict alignment requirement on the target platform. One partial way around this is to wrap the raw storage in a union containing a member of the most restrictivly aligned type you need to use, but since that's platform dependant I'll simply leave that as an exercise for the reader but something like

union
{
char rawdata[1024];
long alignme;
}

should work for the most part.

Share this post


Link to post
Share on other sites
Quote:
Original post by wyrzy
Does anyone know if placement new requires the supplied address to be located on the heap?


No, all that placement new does is invoke the constructor on an area of memory given an address, so it doesn't matter where the memory comes from.

if you use placement new for non POD-types (Plain old data type, a technical term used in the standard) with non trivial destructors (a typical case pretty much all the time). You must explicitly invoke the destructor yourself before deallocation.

I know your dealing with static allocation but if you want to dynamically allocate uninitialized/raw memory in C++ the preferred method is to explicitly invoke operator new e.g.


#include <new> // placement new

void* buf = ::operator new(sizeof(foo)); // allocate only

foo* f = ::new(buf) foo(...); // construct only

//...

f->~foo(); // destroy only, must be done when
// using placement new with NON POD-types.

::operator delete(buf); // deallocate only.

Share this post


Link to post
Share on other sites
Thanks for the replies. I was planning to start on a custom memory manager/allocator, mostly just for the learning exercise and possibly to use in future projects.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this