Create new C++ object at specific memory address?

Chris picture Chris · Oct 12, 2009 · Viewed 28.5k times · Source

Is it possible in C++ to create a new object at a specific memory location? I have a block of shared memory in which I would like to create an object. Is this possible?

Answer

D.Shawley picture D.Shawley · Oct 12, 2009

You want placement new(). It basically calls the constructor using a block of existing memory instead of allocating new memory from the heap.

Edit: make sure that you understand the note about being responsible for calling the destructor explicitly for objects created using placement new() before you use it!