Content deleted Content added
rm bolding in lede, replaced some html entities with unicode, other fixes |
m Unlinking full-dates. Details here. Codes: ISO1(×14) |
||
Line 1:
In the [[C++]] [[programming language]], '''placement syntax''' allows programmers to explicitly specify the [[memory management]] of individual objects — i.e. their "placement" in [[memory (computing)|memory]]. The "placement" versions of the <code>[[new (C++)|new]]</code> and <code>[[delete (C++)|delete]]</code> operators and functions are known as placement <code>new</code> and placement <code>delete</code>.<ref name=McCluskey1>{{cite web|url=http://glenmccl.com./nd_cmp.htm|title=Placement New/Delete|publisher=Glen McCluskey & Associates LLC|work=C++ Language and Library|date=
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 />
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=
== Expressions ==
Line 14:
There is no placement delete expression.<ref name=SolterKleper>{{cite book|title=Professional C++|author=Nicholas Solter and Scott Kleper|pages=458–461|publisher=Wiley|date=2005|isbn=0764574841|isbn13=9780764574849}}</ref>
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=
:<code>new</code> { ''expression-list'' } ''new-type-id'' ( ''optional-initializer-expression-list'' )</code>
Line 162:
=== 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=Anderson1 /><ref name=Yongwei1>{{cite web|title=A Cross-Platform Memory Leak Detector|date=
:<source lang="cpp" enclose=div>
#if defined(DEBUG_NEW)
Line 236:
== 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=
* {{cite web|url=http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/topic/com.ibm.vacpp6m.doc/language/ref/clrc05cplr199.htm|title=C++ new Operator|work=IBM's Mac OS X compilers|date=2003|publisher=[[IBM]]|accessdate=
* {{cite web|url=http://msdn.microsoft.com/en-us/library/t48aek43.aspx|work=[[MSDN]]|title=The operator new Function|publisher=[[Microsoft]]|date=|accessdate=
* {{cite web|url=http://msdn.microsoft.com/en-us/library/kewsb8ba.aspx|work=[[MSDN]]|title=new Operator (C++)|publisher=[[Microsoft]]|date=|accessdate=
* {{cite web|url=http://www.devx.com/tips/Tip/12582|title=Placement new|work=Tip of the day|date=
[[Category:C++]]
|