Placement syntax in the C++ programming language refers to the syntax for explicitly specifying the memory management of individual objects (i.e. their "placement" in memory). Normally, when an object is created dynamically, an allocation function is invoked in such a way that it will both allocate memory for the object, and initialize the object within the newly allocated memory. The placement syntax allows the programmer to supply additional arguments to the allocation function. A common use is to supply a pointer to a suitable region of storage where the object can be initialized, thus separating memory allocation from object construction. The "placement" versions of the new and delete operators and functions are known as placement new and placement delete. A new expression, placement or otherwise, calls a new function, also known as an allocator function, whose name is operator new. Similarly, a delete expression calls a delete function, also known as a deallocator function, whose name is operator delete. Any new expression that uses the placement syntax is a placement new expression, and any operator new or operator delete function that takes more than the mandatory first parameter (size_t) is a placement new or placement delete function. A placement new function takes two input parameters: size_t and void*.
History In earlier versions of C++, there was no such thing as placement new; instead, developers used explicit assignment to this within constructors to achieve similar effect. This practice has been obsolete and removed by C++98, and the third edition of Stroustrup's book, The C++ Programming Language, does not mention this technique.
Expressions The C++ syntax for a non-placement new expression is
new new-type-id ( optional-initializer-expression-list ) The placement syntax adds an expression list immediately after the new keyword. This expression list is the placement. It can contain any number of expressions.
new ( expression-list ) new-type-id ( optional-initializer-expression-list )
Functions The placement new functions are overloads of the non-placement new functions. The declaration of the non-placement new functions, for non-array and array new expressions respectively, are:
The C++ Standard Library provides two placement overloads each for these functions. Their declarations are:
In all of the overloads, the first parameter to the operator new function is of type size_t, which when the function is called will be passed as an argument specifying the amount of memory, in bytes, to allocate. All of the functions must return type void*, which is a pointer to the storage that the function allocates. There are also placement delete functions. They are overloaded versions of the non-placement delete functions. The non-placement delete functions are declared as:
The standard library provides two placement overloads each for these functions. Their declarations are:
In all of the overloads, the first parameter to the operator delete function is of type void*, which is the address of the storage to deallocate. Operators new and delete must reside in the global namespace and cannot have static linkage.
Use Placement syntax has four main uses: default placement, preventing exceptions, custom allocators, and debugging.
Default placement The placement overloads of operator new and operator delete that employ an additional void* parameter are used for default placement, also known as pointer placement. Their definitions by the standard library, which it is not permitted for a C++ program to replace or override, are:
There are various uses for default placement. Bjarne Stroustrup originally observed, in his book The Design and Evolution of C++, that pointer placement new is necessary for hardware that expects a certain object at a specific hardware address. It is also required for the construction of objects that need to reside in a certain memory area, such as an area that is shared between several processors of a multiprocessor computer. Other uses, however, include calling a constructor directly, something which the C++ language does not otherwise permit. The C++ language does allow a program to call a destructor directly, and, since it is not possible to destroy the object using a delete expression, that is how one destroys an object that was constructed via a pointer placement new expression, i.e. p->~X();.
Use cases Placement new is used to prevent operator new from allocating memory (such as if it has been pre-allocated and one wishes to place the object there), but still construct the object. Examples of typical situations where this may be required are:
To create objects in memory shared between two different processes. To ensure objects are created in non-pageable memory. To separate memory allocation from construction e.g. in implementing a std::vector<T> (see the reserve() method). The constructor, unlike other functions, is called when there is no object (only memory), and when it finishes executing, the memory is now a fully initialized object. Therefore, the constructor cannot be called on an object, however, it must access (and initialize) non-static members. This makes calling the constructor directly an error. The solution is the placement form of operator new. This operator is implemented as:
Preventing exceptions Normally, the (non-placement) new functions throw an exception, of type std::bad_alloc, if they encounter an error, such as exhaustion of all available memory. This was not how the functions were defined by Stroustrup's Annotated C++ Reference Manual, but was a change made by the standardization committee when the C++ language was standardized. The original behaviour of the functions, which was to return a null pointer when an error occurred, is accessible via placement syntax. Usage of this requires header <new> from the standard library (or equivalently, module std). This header declares the global std::nothrow object, which is of type std::nothrow_t (also declared in the header), which is used to call the overloaded new functions that are declared as taking const std::nothrow_t& as their second parameter. For example:
… excerpt ends here. Continue reading the full article.