In the C++ programming language, placement syntax allows programmers to explicitly specify 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.

Placement syntax

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 (std::size_t) is a placement new or placement delete function. A placement new function takes two input parameters: std::size_t and void *.

Placement syntax

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 deprecated and abolished later, and the third edition of The C++ Programming Language doesn't mention this technique.

Placement syntax

Expressions

The Standard 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: void* operator new(std::size_t) throw(std::bad_alloc); void* operator new[](std::size_t) throw(std::bad_alloc);

The Standard C++ library provides two placement overloads each for these functions. Their declarations are: void* operator new(std::size_t, const std::nothrow_t&) throw(); void* operator new(std::size_t, void*) throw(); void* operator new[](std::size_t, const std::nothrow_t&) throw(); void* operator new[](std::size_t, void*) throw();

In all of the overloads, the first parameter to the operator new function is of type std::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: void operator delete(void*) throw(); void operator delete[](void*) throw();

The Standard C++ library provides two placement overloads each for these functions. Their declarations are: void operator delete(void*, const std::nothrow_t&) throw(); void operator delete(void*, void*) throw(); void operator delete[](void*, const std::nothrow_t&) throw(); void operator delete[](void*, void*) throw();

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.

For both the new and the delete functions, the functions are global, are not in any namespace, and do not 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 C++ library, which it is not permitted for a C++ program to replace or override, are:void* operator new(std::size_t, void* p) throw() { return p; } void* operator new[](std::size_t, void* p) throw() { return p; } void operator delete(void*, void*) throw() { } void operator delete[](void*, void*) throw() { }

There are various uses for default placement.