Search code examples
c++memoryobjectpointerslocation

Create new C++ object at specific memory address?


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?


Solution

  • 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!