Tag Archives: placement new

Placement new in C++

Placement new in C++ in the form

object* pointer = new (pre-allocated buffer) object;

allows the programmer to allocate an object dynamically onto a pre-allocated memory address. Unlike classical new keyword in C++, placement new does not actually create any memory space in heap.

In the code above, an object of type object is created and allocated onto the address pre-allocated buffer, then the address to where it is stored is returned and stored into the variable pointer.

Example use case:

Car* c = new (nothrow) Car("BMW");
if (c == nullptr) printf("Memory allocation failed\n");
else {
    printf("Current car is %s\n", c->type());
    new (c) Car("Toyota");
    printf("New car is %s\n", c->type());
    delete c;
    Car local_car;
    new (&local_car) Car("Honda");
    printf("Your local car is %s\n", local_car->type());
    local_car->~Car();
}

In the above example, a Car object has been dynamically created in heap (line 1), and nothrow means do not throw an exception if the allocation failed, this allows the program to proceed even if there is a memory allocation error. Assume type() returns the name of the car, line 4 will print Current car is BMW.

On line 5, placement new is being called, thus, a new Car object has been created and allocated on the address that c points to. As a result, line 6 prints New car is Toyota.

To delete an object that is created with placement new, you do it the same way as how you delete a typical dynamically created object, you use delete. (line 7).

Another usage for placement new is that it allows programmer to allocate object on stack. On line 8, a Car object is created locally on stack, since no string is passed into the constructor, default constructor is being called. However, on line 9, placement new is being called, and this time, the newly created Car object Honda is stored into the local_car that is located in stack. Since objects created locally or in stack will automatically be deallocated, we do not need to call delete this time. However, you can call destructor if you want to free the memory that is created dynamically in side of the object.

When should you use it?

  • When a memory space is already allocated, you can keep allocating object on the allocated splace without keep creating new one. This allows the program to run faster since it saves the time to deallocate unused memory and reallocate a new one.
  • When you want to make sure the memory you are using is valid (since placement new uses pre-allocated memory, is should always be valid).
  • When you are programming on to a limited memory device, then you should always track the memory you are using, and reuse it as much as you can to prevent fragmentation or seg fault.

Things you should consider when using placement new

  • Make sure the memory you are allocating on is a valid pointer (not null, and has enough space).
  • Remember to deallocate it (or use smart pointer).
  • You should take care of casting if you are allocating an object on an memory that was created for other purpose.
  • Make sure it points to a valid location, by valid, I mean a location you are allowed to access and use.