Content deleted Content added
This is the actual URL. |
Have some more stuff. |
||
Line 9:
A new ''expression'', placement or otherwise, calls a new ''function'', also known as an allocator function, whose name is <code>operator new</code>. Similarly, a delete ''expression'' calls a delete ''function'', also known as a deallocator function, whose name is <code>operator delete</code>.<ref name=Lischner1 /><ref name=Lippman1 />
Any <code>new</code> expression that uses the placement syntax is a placement <code>new</code> expression, and any <code>operator new</code> or <code>operator delete</code> function that takes more than the mandatory first parameter (<source lang="cpp" enclose=none>size_t</source> and <source lang="cpp" enclose=none>void *</source>, respectively) is a placement new or placement delete function.<ref name=DDJMeyers1998>{{cite news|date=[[1998-04-01]]|author=Scott Meyers|title=Placement new and placement delete|url=http://ddj.com//showArticle.jhtml?documentID=cuj9804meyers&pgno=2|work=[[Dr. Dobb's Journal]]|publisher=United Business Media LLC}}</ref>
== Expressions ==
The Standard C++ syntax for a non-placement <code>new</code> expression is<ref name=Lischner1>{{cite book|title=C++ in a Nutshell|author=Ray Lischner|pages=72–73,128–129,310,623–625|publisher=O'Reilly|date=2003|isbn=059600298X|isbn13=9780596002985}}</ref>
Line 82 ⟶ 83:
</source>
=== Preventing exceptions ===
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 <source lang="cpp" enclose=none>NULL</source> pointer when an error occurred, is accessible via placement syntax.<ref name=Lippman1 /><ref name=Loudon1 /><ref name=DDJMeyers1998 />
Programmers that 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 <source lang="cpp" enclose=none>const nothrow_t &</source> as their second parameter. For example:<ref name=Anderson1 />
Line 206 ⟶ 207:
As noted above, there is no placement delete expression. It is not possible to call ''any'' placement <code>operator delete</code> function using a <code>delete</code> expression.<ref name=SolterKleper />
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=SolterKleper /><ref name=Lischner1 /><ref name=DDJMeyers1998 />
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=SolterKleper /><ref name=Lischner1 />
|