The funny syntax inside the try statement is called "placement new", and we’ll discuss it shortly. For completeness’ sake, let’s see a similar breakdown for freeing an object with delete, which is also a two-step process:

First, the destructor of the object that’s being deleted is called.

Then, the memory occupied by the object is returned to the OS, represented by the global operator delete function.

So:

delete p;

Is equivalent to:

if (p != NULL) {
p->~Foo();
operator delete(p);
}

Note the check for NULL. It's the reason for delete p being safe even when p is NULL – another C++ FAQ.

This is also a good place to repeat something I’ve mentioned in the first section of this article – if a class has its own operator new or operator delete, these get invoked instead of the global functions when an object is allocated or deallocated.

Placement new

Now, back to that "placement new" we saw in the code sample above. It happens to be a real syntax we can use in our C++ code. First, I want to briefly explain how it works. Then, we’ll see when it can be useful.

Calling placement new directly skips the first step of object allocation. We don’t ask for memory from the OS. Rather, we tell it where there’s memory to construct the object in. Remember, to ensure that the location that pointer goes to has enough memory for the object, and that the pointer is also correctly aligned. The following code sample should clarify this:

As you can see, the mechanics of placement new are quite simple. What’s more interesting is the question – why would we need something like this? It turns out placement new is quite useful in a few scenarios:

Custom non-intrusive memory management. While overloading operator new for a class also allows custom memory management, the key concept here is non-intrusive. Overloading operator new requires you to change the source code of a class. But suppose we have a class the code of which we don’t want or can’t change. How can we still control its allocation? Placement new is the answer here. A common programming technique that uses placement new for this purpose is memory pools, sometimes also called "arenas" (Memory pools is a large and fascinating topic that I can’t cover it in any meaningful depth here. So I encourage you to look up more information such as Wikipedia's)

In some applications it is necessary to allocate objects in specific memory regions. One example is shared memory. Another is embedded applications or drivers with memory mapped peripherals, which can be controlled conveniently by objects allocated "on top" of them.

Many container libraries pre-allocate large buffers of memory. When new objects are added they have to be constructed in these buffers, so placement new is used. The prime example is probably the standard vector container.