Pages

Tuesday, November 30, 2010

What is placement new

Placement new is used to create object from an pre allocated buffer.

We all know to new create an object and allocates memory from the heap.

Placement new allocated memory from a pre allocated buffer.

For example -
char *buf = new char[1000];

string *p = new (buf) string ("hi"); // pre allocated buffer
string *q = nre string("hello"); // normal heap allocation

No comments: