Content deleted Content added
→Expressions: merged small sections |
rm bolding in lede, replaced some html entities with unicode, other fixes |
||
Line 1:
In the [[C++]] [[programming language]], '''placement syntax''' allows programmers to explicitly specify the [[memory management]] of individual objects
A <code>new</code> ''expression'', placement or otherwise, calls a <code>new</code> ''function'', also known as an allocator function, whose name is <code>operator new</code>. Similarly, a <code>delete</code> ''expression'' calls a <code>delete</code> ''function'', also known as a deallocator function, whose name is <code>operator delete</code>.<ref name=Lischner1 /><ref name=Lippman1 />
Line 6:
== 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=
:<code>new</code> ''new-type-id'' ( ''optional-initializer-expression-list'' )</code>
The placement syntax adds an expression list immediately after the <code>new</code> keyword. This expression list is the placement. It can contain any number of expressions.<ref name=Lischner1 /><ref name=Lippman1>{{cite book|title=C++ Gems|author=Stanley B. Lippman|pages=
:<code>new</code> ( ''expression-list'' ) ''new-type-id'' ( ''optional-initializer-expression-list'' )</code>
There is no placement delete expression.<ref name=SolterKleper>{{cite book|title=Professional C++|author=Nicholas Solter and Scott Kleper|pages=
Versions of the [[g++]] compiler prior to version 2.3.1 accept a different syntax for placement new expressions:<ref name=Buck1>{{cite web|url=http://desy.de./user/projects/C++/g++faq/placement_new_syntax.html|title=3.4. g++ won't accept the placement new syntax.|date=[[1997-05-12]]|author=Joe Buck|work=Frequently asked questions about the GNU C++ compiler|accessdate=[[2008-11-26]]}}</ref>
Line 20:
== 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=Vermeir1>{{cite book|title=Multi-paradigm Programming Using C++|author=Dirk Vermeir|pages=
:<source lang="cpp" enclose=div>
void * operator new (size_t) throw(std::bad_alloc);
Line 57:
Placement syntax has four main uses: default placement, preventing exceptions, custom allocators, and debugging.
=== Default placement ===
The placement overloads of <code>operator new</code> and <code>operator delete</code> that employ an additional <source lang="cpp" enclose=none>void *</source> 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=Vermeir1 /><ref name=TCPPPL /><ref name=Anderson1>{{cite book|title=Navigating C++ and Object-oriented Design|author=Gail Anderson|chapter=Object Storage Management|pages=
:<source lang="cpp" enclose=div>
void * operator new (size_t, void * p) throw() { return p ; }
Line 73:
Other uses, however, include calling a constructor directly, something which the C++ language does not otherwise permit.<ref name=Lippman1 />
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=SeedCooper>{{cite book|title=An Introduction to Object-oriented Programming in C++|author=Graham M. Seed and Barry J. Cooper|pages=
:<source lang="cpp" enclose=div>
p->~T() ;
Line 117:
return arena.allocate(size) ;
}
void
operator delete (void * p, A & arena)
{
Line 135:
The former would resemble:<ref name=TCPPPL />
:<source lang="cpp" enclose=div>
void
destroy (T * p, A & arena)
{
Line 150:
</source>
The latter would involve simply writing the destructor invocation and delete function call into the program:<ref name=Vermeir1 /><ref name=Dewhurst>{{cite book|title=C++ Gotchas|author=Stephen C. Dewhurst|chapter=Gotcha #62: Replacing Global New and Delete|chapterurl=http://informit.com./articles/article.aspx?p=30642&seqNum=3|pages=
:<source lang="cpp" enclose=div>
A arena ;
Line 191:
} ;
void *
operator new (size_t size, const char* file, int line)
{
Line 200:
</source>
== Placement delete ==
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 /><ref name=Anderson2>{{cite book|title=Navigating C++ and Object-oriented Design|author=Gail Anderson|chapter=Exception Handling|pages=
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 /><ref name=Anderson2 />
Line 231:
If no matching placement delete function exists, no deallocation function is called in the event of an exception being thrown by a constructor within a placement <code>new</code> expression. There are also some (older) C++ implementations that do not support placement delete (which, like the exception-throwing allocator functions, were an addition made to C++ when it was standardized) at all. In both such situations, an exception being thrown by a constructor when allocating using a custom allocator will result in a memory leak. (In the case of the older C++ implementations, a memory leak will also occur with ''non-''placement <code>new</code> expressions.)<ref name=DDJMeyers1998 /><ref name=Anderson2 />
== References ==
{{reflist|2}}
== Further reading ==
* {{cite book|last=Franek|first=Frantisek|title=Memory as a Programming Concept in C and C++|publisher=[[Cambridge University Press]]|isbn=9780521520430|year=2004}}
* {{cite web|title=11.10: What is "placement new" and why would I use it?|url=http://parashift.com./c++-faq-lite/dtors.html#faq-11.10|work=C++ FAQ Lite|date=[[2006-09-25]]|author=Marshall Cline|accessdate=[[2008-11-26]]}}
|