Placement new operator in C++

Last Updated : 22 Jun, 2026

Placement new is a special form of the new operator that constructs an object in a programmer-supplied memory location instead of allocating memory from the heap.

  • Constructs objects in an already allocated memory block.
  • Separates memory allocation from object construction, giving greater control over object placement.

Syntax

Placement new constructs an object at a specified memory address.

new (address) Type(arguments);

where,

  • address is the location where the object will be created.
  • Type is the object type.
  • arguments are passed to the constructor.

Constructing Objects Using Placement new 

Placement new constructs objects inside an existing memory block instead of allocating memory from the heap. The memory must already be available before the object is created.

  • Uses a preallocated memory buffer for object construction.
  • Returns a pointer to the object created at the specified memory location.

Example: The following program constructs two integers inside a preallocated buffer using placement new.

CPP
#include<iostream>
using namespace std;

int main()
{
    // buffer on stack
    unsigned char buf[sizeof(int)*2] ;

    // placement new in buf
    int *pInt = new (buf) int(3);

    int *qInt = new (buf + sizeof (int)) int(5);
    int *pBuf = (int*)(buf+0) ;
    int *qBuf = (int*) (buf + sizeof(int));
    cout << "Buff Addr             Int Addr" << endl;
    cout << pBuf <<"             " << pInt << endl;
    cout << qBuf <<"             " << qInt << endl;
    cout << "------------------------------" << endl;
    cout << "1st Int             2nd Int" << endl;
    cout << *pBuf << "                         "
         << *qBuf << endl;

    return 0;
}

Output

Buff Addr              Int Addr
0x69fed8 0x69fed8
0x69fedc 0x69fedc
----------------------------
1st Int 2nd Int
3 5

Explanation

  • buf is a preallocated memory buffer on the stack.
  • new (buf) int(3) constructs an integer at the beginning of the buffer.
  • new (buf + sizeof(int)) int(5) constructs another integer immediately after the first.
  • Since no new memory is allocated, the object addresses are exactly the same as the supplied buffer addresses.

The diagram below pictorially shows what is actually happening in above C++ program.
 

Placement new operator in C++ 1

Constructing an Object at a Specific Address 

Placement new can also construct an object at the address of an existing memory location instead of allocating new memory.

  • Constructs an object at a programmer-specified address.
  • The returned pointer refers to the same memory location supplied to placement new.

Example: The following program constructs an integer at the address of an existing variable.

CPP
#include<iostream>
using namespace std;
int main()
{
    // initial value of X
    int X = 10;

    cout << "Before placement new :" << endl;
    cout << "X : " << X << endl;
    cout << "&X : " << &X << endl;

    // Placement new changes the value of X to 100
    int *mem = new (&X) int(100);

    cout << "\nAfter placement new :" << endl;
    cout << "X : " << X << endl;
    cout << "mem : " << mem << endl;
    cout << "&X : " << &X << endl;

    return 0;
}

Output 

Before placement new :
X : 10
&X : 0x69fee8

After placement new :
X : 100
mem : 0x69fee8
&X : 0x69fee8

Explanation 

  • &X is passed as the memory location.
  • Placement new constructs an integer with value 100 at that address.
  • Both mem and &X point to the same location.
Placement new operator in C++ 2

Destroying Objects Created Using Placement new 

Objects created with placement new are not allocated from the heap, so they must not be destroyed using delete .

  • Use delete only for objects created with the normal new operator.
  • For placement new, explicitly invoke the destructor when the object is no longer required.

Example: The following program demonstrates manually calling the destructor for an object created using placement new.

CPP
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;

class Complex
{
private:
    double re_, im_;
public:
    // Constructor
    Complex(double re = 0, double im = 0): re_(re), im_(im)
    {
        cout << "Constructor : (" << re_
             << ", " << im_ << ")" << endl;
    }

    // Destructor
    ~Complex()
    {
        cout << "Destructor : (" << re_ << ", "
             << im_ << ")" << endl;
    }

    double normal()
    {
        return sqrt(re_*re_ + im_*im_);
    }

    void print()
    {
        cout << "|" << re_ <<" +j" << im_
             << " | = " << normal() << endl;
    }
};

// Driver code
int main()
{
    // buffer on stack
    unsigned char buf[100];

    Complex* pc = new Complex(4.2, 5.3);
    Complex* pd = new Complex[2];

    // using placement new
    Complex *pe = new (buf) Complex(2.6, 3.9);

    // use objects
    pc -> print();
    pd[0].print();
    pd[1].print();
    pe->print();

    // Release objects
    // calls destructor and then release memory
    delete pc;

    // Calls the destructor for object pd[0]
    // and then release memory
    // and it does same for pd[1]
    delete [] pd;

    // No delete : Explicit call to Destructor.
    pe->~Complex();

    return 0;
}

Output

Constructor : (4.2, 5.3)
Constructor : (0, 0)
Constructor : (0, 0)
Constructor : (2.6, 3.9)
|4.2 +j5.3 | = 6.7624
|0 +j0 | = 0
|0 +j0 | = 0
|2.6 +j3.9 | = 4.68722
Destructor : (4.2, 5.3)
Destructor : (0, 0)
Destructor : (0, 0)
Destructor : (2.6, 3.9)

Explanation 

  • pc and pd are allocated using normal new, so delete and delete[] are used.
  • pe is constructed inside an existing buffer using placement new.
  • Since placement new does not allocate memory, only the destructor is called explicitly.

Applications of Placement new

Placement new is commonly used when object construction needs to be separated from memory allocation.

  • Constructing objects inside memory pools.
  • Building custom allocators.
  • Implementing garbage collectors.
  • Developing embedded systems with limited memory.
  • High-performance applications where frequent heap allocations are expensive.

Advantages of Placement new

Placement new provides several advantages over the normal new operator.

  • Constructs objects in an already allocated memory block.
  • Eliminates repeated heap allocations.
  • Allows precise control over object placement.
  • Useful for performance-critical and low-level programming.
  • Suitable for memory pools and custom memory management.

Limitations of Placement new

Placement new should be used carefully because memory management becomes the programmer's responsibility.

  • The supplied address must refer to a valid memory location.
  • It does not allocate or free memory.
  • Objects must be destroyed manually by calling their destructor when required.
  • Using an invalid or uninitialized address results in undefined behavior.

Placement new Vs Normal new

The following table highlights the differences between the normal new operator and placement new.

Normal newPlacement new
Allocates memory and constructs the object.Constructs the object in already allocated memory.
Memory is allocated from the heap.Memory location is supplied by the programmer.
Returns a pointer to newly allocated memory.Returns the same address that was provided.
Memory is released using delete.Memory is not released automatically; only the destructor is called explicitly if required.
May fail due to insufficient heap memory.Does not allocate memory, so allocation failure is not possible.
Comment