This syntax is called the placement new syntax. It is usually used to construct an object in a pre-allocated buffer. This is useful when creating a memory pool, garbage collector, or simply when safety of performance and exceptions is paramount (there is no danger of distribution failure, because memory is already allocated, and building an object in a previously allocated buffer takes less time).
char *buf = new char[sizeof(string)]; // pre-allocated buffer string *s1 = new (buf) string("test1"); // placement new string *s2 = new string("test2"); //ordinary new
When it comes to liberation, there is no placement delete that automatically does the magic. You do not have to free all objects that use the memory buffer. Instead, you must destroy each object manually, and then delete [] only the source buffer
source share