Placement new operator in C++



In C++, we allocate the memory dynamically using the new operator. But there is a special version of this operator known as placement new operator.

The new operator performs two things. It allocates memory, and then constructs an object in allocated memory. But for the placement new operator, it constructs object at the given address.

What is Placement New?

The placement new operator allows you to construct an object at a specific memory location. Its syntax lets you place an object at a pre-allocated memory address instead of letting the compiler allocate memory.

new (address) Type (constructor_args);

Where the address is a pointer to the pre-allocated memory, and the type is the type of object you want to create.

Example of Placement New with Raw Buffer Allocation in C++

Here is a basic example. We use the placement new operator to allocate memory ?

#include <iostream>
using namespace std;
int main() {
   // Allocate raw memory buffer
   char buffer[sizeof(int)];
   
   // Construct an int at the buffer location
   int* p = new (buffer) int(42);
   
   cout << "Value: " << *p << endl;
   cout << "Address: " << static_cast<void*>(buffer) << " == " << p << endl;
   return 0;
}

The following is the output of the above code ?

Value: 42
Address: 0x7ffe28ab4534 == 0x7ffe28ab4534

Example of Overwriting Value at an Existing Address

In the following example, we are overwriting the value at an existing address using the placement new operator ?

#include<iostream>
using namespace std;
int main() {
   int a = 5;
   cout << "a = " << a << endl;
   cout << "&a = " << &a << endl;
   // Placement new changes the value of X to 100
   int *m = new (&a) int(10);
   cout << "\nAfter using placement new:" << endl;
   cout << "a = " << a << endl;
   cout << "m = " << m << endl;
   cout << "&a = " << &a << endl;
   return 0;
}

Following is the output of the above code ?

a = 5
&a = 0x7fff5966d12c

After using placement new:
a = 10
m = 0x7fff5966d12c
&a = 0x7fff5966d12c

Note: In the above code we are not allocating new memory. we are reusing memory that already belongs to variable a.

Example

This is another example of the placement new operator. Here, we construct an object in pre-allocated memory ?

#include <iostream>
using namespace std;
class Person {
    string name;
    int age;
public:
   Person(string n, int a) : name(n), age(a) {
      cout << "Constructor called: " << name << ", " << age << endl;
   }
   ~Person() {
      cout << "Destructor called: " << name << endl;
   }
   void display() {
      cout << "Name: " << name << ", Age: " << age << endl;
   }
};
int main() {
   // Allocate raw memory for a Person object
   void* mem = operator new(sizeof(Person));
   
   // Construct a Person in that memory using placement new
   Person* p = new (mem) Person("Aman", 25);
   
   p->display();
   
   p->~Person();
   
   //Free the raw memory
   operator delete(mem);
   return 0;
}

The above code generates the following output ?

Constructor called: Aman, 25
Name: Aman, Age: 25
Destructor called: Aman
Updated on: 2025-06-18T18:33:22+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements