Content deleted Content added
m remove default positional parameter (via WP:JWB) |
Add category |
||
Line 16:
== 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 <code>new</code> expressions respectively, are:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> </syntaxhighlight>
The Standard C++ library provides two placement overloads each for these functions. Their declarations are:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> </syntaxhighlight>
In all of the overloads, the first parameter to the <code>operator new</code> function is of type <syntaxhighlight lang="cpp" inline>std::size_t</syntaxhighlight>, 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 <syntaxhighlight lang="cpp" inline>void *</syntaxhighlight>, which is a [[pointer (computer programming)|pointer]] to the storage that the function allocates.<ref name=Lischner2003 />
There are also placement delete functions. They are overloaded versions of the non-placement delete functions. The non-placement delete functions are declared as:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> </syntaxhighlight>
The Standard C++ library provides two placement overloads each for these functions. Their declarations are:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> </syntaxhighlight>
Line 50 ⟶ 54:
=== Default placement ===
The placement overloads of <code>operator new</code> and <code>operator delete</code> that employ an additional <syntaxhighlight lang="cpp" inline>void *</syntaxhighlight> 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:<ref name=Vermeir2001 /><ref name=Stroustrup1997b /><ref name=Anderson1998a />
<syntaxhighlight lang="cpp"> void* operator new(std::size_t, void* p) throw() { return p; }
void* operator new[](std::size_t, void* p) throw() { return p; }
Line 64 ⟶ 69:
The C++ language does allow a program to call a [[destructor (computer science)|destructor]] directly, and, since it is not possible to destroy the object using a <code>delete</code> expression, that is how one destroys an object that was constructed via a pointer placement new expression. For example:<ref name=SolterKleper2005 /><ref name=SeedCooper2001 />
p->~T();
</syntaxhighlight>
Line 76 ⟶ 81:
The basic problem is that the constructor is a peculiar function; when it starts off, there is no object, only raw memory. And by the time it finishes, you have a fully initialized object. Therefore, i) The constructor cannot be called on an object ii) However, it needs to 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:
<syntaxhighlight lang="cpp"> ▲ void* operator new[](std::size_t count, void* here) { return here; }
</syntaxhighlight>
Line 85 ⟶ 90:
Normally, the (non-placement) new functions throw an exception, of type <code>std::bad_alloc</code>, 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 <syntaxhighlight lang="cpp" inline>NULL</syntaxhighlight> pointer when an error occurred, is accessible via placement syntax.<ref name=Lippman1997 /><ref name=Meyers1998 /><ref name=Loudon2003 />
Programmers who wish to do this in their programs must include the Standard C++ library header <code><new></code> in the source code. This header declares the global <code>std::nothrow</code> object, which is of type <code>std::nothrow_t</code> (also declared in the header), which is used to call the overloaded new functions that are declared as taking <syntaxhighlight lang="cpp" inline>const std::nothrow_t &</syntaxhighlight> as their second parameter. For example:<ref name=Anderson1998a />
<syntaxhighlight lang="cpp"> #include <new>
Line 103 ⟶ 109:
=== Custom allocators ===
Placement syntax is also employed for custom [[allocator (C++)|allocators]]. This does not use any of the allocator and deallocator functions from the Standard C++ library header <code><new></code>, but requires that programmers write their own allocation and deallocation functions, overloaded for user-defined types. For example, one could define a memory management class as follows:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> #include <cstdlib>
class A {
Line 112 ⟶ 119:
</syntaxhighlight>
And define custom placement allocation and deallocation functions as follows:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> void* operator new(std::size_t size, A& arena) {
return arena.allocate(size);
Line 122 ⟶ 130:
</syntaxhighlight>
The program would employ the placement syntax to allocate objects using different instances of the <code>A</code> class as follows:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> A first_arena, second_arena;
T* p1 = new(first_arena) T;
Line 130 ⟶ 139:
Destroying an object whose storage is allocated in such a fashion requires some care. Because there is no placement delete expression, one cannot use it to invoke the custom deallocator. One must either write a destruction function that invokes the custom deallocator, or call the placement delete function directly, as a function call.<ref name=SolterKleper2005 /><ref name=Vermeir2001 /><ref name=Stroustrup1997b />
The former would resemble:<ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> void destroy(T* p, A& arena) {
p->~T(); // First invoke the destructor explicitly.
Line 136 ⟶ 146:
}
</syntaxhighlight>
which would be invoked from a program as:
<syntaxhighlight lang="cpp"> A arena;
T* p = new(arena) T;
Line 143 ⟶ 154:
</syntaxhighlight>
The latter would involve simply writing the destructor invocation and delete function call into the program:<ref name=Vermeir2001 /><ref name=Dewhurst2003 />
<syntaxhighlight lang="cpp"> A arena;
T* p = new(arena) T;
Line 154 ⟶ 166:
=== Debugging ===
Placement new can also be used as a simple debugging tool, to enable programs to print the filename and line number of the source code where a memory allocation has failed. This does not require the inclusion of the Standard C++ library header <code><new></code>, but does require the inclusion of a header that declares four placement functions and a macro replacement for the <code>new</code> keyword that is used in new expressions. For example, such a header would contain:<ref name=Anderson1998a /><ref name=Yongwei2007 />
<syntaxhighlight lang="cpp"> #if defined(DEBUG_NEW)
void* operator new(std::size_t size, const char* file, int line);
Line 166 ⟶ 179:
</syntaxhighlight>
This would be employed in a program as follows:<ref name=Anderson1998a /><ref name=Yongwei2007 />
<syntaxhighlight lang="cpp"> T* p = New T;
</syntaxhighlight>
The custom-written placement new functions would then handle using the supplied file and line number information in the event of an exception. For example:<ref name=Anderson1998a /><ref name=Yongwei2007 />
<syntaxhighlight lang="cpp"> #include <new>
#include <cstdlib>
Line 194 ⟶ 209:
The placement delete functions are called from placement <code>new</code> expressions. In particular, they are called if the [[constructor (computer science)|constructor]] of the object throws an exception. In such a circumstance, in order to ensure that the program does not incur a [[memory leak]], the placement delete functions are called. A placement new expression first calls the placement <code>operator new</code> function, then calls the constructor of the object upon the raw storage returned from the allocator function. If the constructor throws an exception, it is necessary to deallocate that storage before propagating the exception back to the code that executed the placement new expression, and that is the purpose of the placement delete functions.<ref name=Lischner2003 /><ref name=Meyers1998 /><ref name=SolterKleper2005 /><ref name=Anderson1998b />
The placement delete function that is called matches the placement new function that was invoked by the placement new expression. So, for example, if the following code is executed, the placement delete function that is called will be <code>operator delete(void *, const A &)</code>:<ref name=Lischner2003 /><ref name=SolterKleper2005 /><ref name=Anderson1998b />
<syntaxhighlight lang="cpp"> #include <cstdlib>
#include <iostream>
Line 280 ⟶ 296:
{{DEFAULTSORT:Placement Syntax}}
[[Category:Articles with example C++ code]]
[[Category:C++]]
|