R@M3$H.NBlog

Placement new

28 September, 2013 - 3 min read

There are many uses of placement new. The simplest use is to place an object at a particular location in memory. This is done by supplying the place as a pointer parameter to the new part of a new expression:

#include <new>        // Must #include this to use "placement new"
#include "Fred.h"     // Declaration of class Fred

void someCode()
{
  char memory[sizeof(Fred)];     // Line #1
  void* place = memory;          // Line #2

  Fred* f = new(place) Fred();   // Line #3 (see "DANGER" below)
  // The pointers f and place will be equal

  ...
}

Line #1 creates an array of sizeof(Fred) bytes of memory, which is big enough to hold a Fred object. Line #2 creates a pointer place that points to the first byte of this memory (experienced C programmers will note that this step was unnecessary; it's there only to make the code more obvious). Line #3 essentially just calls the constructor Fred::Fred(). The this pointer in the Fred constructor will be equal to place. The returned pointer f will therefore be equal to place.

ADVICE: Don't use this "placement new" syntax unless you have to. Use it only when you really care that an object is placed at a particular location in memory. For example, when your hardware has a memory-mapped I/O timer device, and you want to place a Clock object at that memory location.

DANGER: You are taking sole responsibility that the pointer you pass to the "placement newoperator points to a region of memory that is big enough and is properly aligned for the object type that you're creating. Neither the compiler nor the run-time system make any attempt to check whether you did this right. If your Fred class needs to be aligned on a 4 byte boundary but you supplied a location that isn't properly aligned, you can have a serious disaster on your hands (if you don't know what "alignment" means, please don't use the placement new syntax). You have been warned.

You are also solely responsible for destructing the placed object. This is done by explicitly calling the destructor:

void someCode()
{
  char memory[sizeof(Fred)];
  void* p = memory;
  Fred* f = new(p) Fred();
  ...
  f->~Fred();   // Explicitly call the destructor for the placed object
}

This is about the only time you ever explicitly call a destructor.

What is placement new in C++?

  • In some scenarios it becomes necessary to place objects at specified locations in memory. Placement new solves this problem.
  • For example, there could be predefined memory pools in which certain objects need to be placed on creation.
  • Placement new is achieved by overloading the new operator which takes more than one argument apart from the default size_t argument.

EXAMPLE: Demonstrate the usage of placement new

#include &lt;iostream&gt;
using namespace std;

class MyClass {

   public:

    // Placement new operator
    void* operator new (size_t sz, void* v) {
        cout &lt;&lt; "Placement new invoked" &lt;&lt; endl;
        return v;
    }

    ~MyClass() {
        // Cleanup
    }
};

int main()
{
    // Create a buffer to store the object
    int buffer[16];
    cout &lt;&lt; "Starting address of my buffer = " &lt;&lt; &amp;buffer &lt;&lt; endl;

    // Create the object. Use placement new
    MyClass* obj = new (buffer) MyClass();
    cout &lt;&lt; "Location of my object = " &lt;&lt; obj &lt;&lt; endl;

    // Don't delete object created with placement delete
    // Call the destructor explicitly
    obj-&gt;~MyClass();
}

OUTPUT:
Starting address of my buffer = 0012FF4C
Placement new invoked
Location of my object = 0012FF4C

 

http://blog.aaronballman.com/2011/08/the-placement-new-operator/

END