What is the difference between the two statements
The code construct:
new (<address>) <typename>{ <arguments> };
is called "placement new". Contrary to the default new
(i.e. same without the address), this call doesn't allocate memory at all, but assumes the <address>
is to already allocated memory (that is large enough) and simply constructs the object at the provided address.
This is not the only special case for the new
operator. You can also pass an allocator, or some other (random) contextual information to new.
In order for new to support this (passing other information to the operator) you must define a custom operator new
implementation, that receives the extra information as parameters. To see in detail how to do this, check online (search for "custom operator new
", "placement new" and "define custom operator new"). Note that implementing a custom new operator has some pitfalls for implementing correctly. This is why you should read more about it if you plan to define it.
The standard library, implements by default new, placement new and (I think) a new that supports a std::allocator instance.