Content deleted Content added
No edit summary Tag: Reverted |
Citation bot (talk | contribs) Removed URL that duplicated identifier. | Use this bot. Report bugs. | Suggested by Headbomb | Linked from Wikipedia:WikiProject_Academic_Journals/Journals_cited_by_Wikipedia/Sandbox | #UCB_webform_linked 104/967 |
||
(19 intermediate revisions by 15 users not shown) | |||
Line 1:
In the [[C++]] [[programming language]], '''placement [[Syntax (programming languages)|syntax]]''' allows programmers to explicitly specify the [[memory management]] of individual objects — i.e. their "placement" in [[memory (computing)|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 [[Constructor (object-oriented programming)|initialize]] the object within the newly allocated memory.<!--5.3.5--> The placement syntax allows the programmer to supply additional arguments to the allocation function.
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=McCluskey2000 /> 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=Lischner2003 /><ref name=Lippman1997 />
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 (<syntaxhighlight lang="cpp" inline>std::size_t</syntaxhighlight>) is a placement new or placement delete function.<ref name=Meyers1998 /> A placement new function takes two input parameters: <syntaxhighlight lang="cpp" inline>std::size_t</syntaxhighlight> and <syntaxhighlight lang="cpp" inline>void
== History ==
In earlier versions of C++, there was no such thing as ''placement new''; instead, developers used explicit assignment to
== Expressions ==
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
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>
In all of the overloads, the first parameter to the <code>operator delete</code> function is of type <syntaxhighlight lang="cpp" inline>void
For both the new and the delete functions, the functions are global, are not in any namespace, and do not have static linkage.<ref name=Lischner2003 />
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 lang="cpp"> void
void
void operator delete
void operator delete[]
</syntaxhighlight>
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>
=== Use cases ===
Placement new is used when you do not want operator new to allocate memory (you have pre-allocated it and you want to place the object there), but you do want the object to be constructed. Examples of typical situations where this may be required are:
* You want to create objects in memory shared between two different processes.
* You want objects to be created in non-pageable memory.
* You want to separate memory allocation from construction e.g. in implementing a <code>std::vector<></code> (see <code>std::vector<>::reserve</code>).
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"> { return here ; }▼
▲ inline void* operator new[]( size_t sz, void* here )
</syntaxhighlight>
Line 86 ⟶ 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>
<syntaxhighlight lang="cpp"> import std;
struct T {}
int main
// Call the function operator new(std::size_t, const std::nothrow_t
▲ // Call the function operator new(std::size_t, const std::nothrow_t &) and (if successful) construct the object.
▲ T * p = new (std::nothrow) T ;
if (p) {
// The storage has been allocated and the constructor called.
delete p
} else
; // An error has occurred. No storage has been allocated and no object constructed.
return 0
}
</syntaxhighlight>
=== 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>
<syntaxhighlight lang="cpp"> import std;
class A {
public:
void
void deallocate
}
</syntaxhighlight>
And define custom placement allocation and deallocation functions as follows:<ref name=Vermeir2001 /><ref name=Stroustrup1997b />
<syntaxhighlight lang="cpp"> ▲operator new (std::size_t size, A & arena)
▲ return arena.allocate(size) ;
}
void operator delete
▲ arena.deallocate(p) ;
}
</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
T
</syntaxhighlight>
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"> ▲destroy (T * p, A & arena)
▲ p->~T() ; // First invoke the destructor explicitly.
▲ arena.deallocate(p) ; // Then call the deallocator function directly.
}
</syntaxhighlight>
which would be invoked from a program as:
<syntaxhighlight lang="cpp"> A arena
T
/* ... */
destroy(p, arena)
</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->~T()
operator delete(p, arena)
</syntaxhighlight>
Line 161 ⟶ 167:
=== 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
void
void operator delete
void operator delete[]
#define New new(__FILE__, __LINE__)
#else
Line 173 ⟶ 180:
</syntaxhighlight>
This would be employed in a program as follows:<ref name=Anderson1998a /><ref name=Yongwei2007 />
<syntaxhighlight lang="cpp"> 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"> import std;
class
public:
/* ... */
} ;
▲operator new (std::size_t size, const char* file, int line)
▲ if (void * p = ::operator new (size, std::nothrow))
▲ return p ;
▲ throw NewError(file, line) ;
}
</syntaxhighlight>
Line 201 ⟶ 207:
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
<syntaxhighlight lang="cpp"> import std;
struct A {}
struct E {}
class T {
public:
T() { throw E()
}
void
}
void operator delete ( void *, const A & )▼
{std::cout << "Placement delete called." << std::endl;}▼
int main ()▼
A a ;▼
try {▼
T * p = new (a) T ;▼
} catch (E exp) {std::cout << "Exception caught." << std::endl;}▼
}
▲ try {
} catch (E exp) {
}
}
</syntaxhighlight>
This is why the ''pointer placement'' delete functions are defined as no-operations by the Standard C++ library. Since the pointer placement new functions do not allocate any storage, there is no storage to <ref name=":0">{{Cite
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=Meyers1998 /><ref name=Anderson1998b />
== Security ==
Placement new expressions are vulnerable to security exploits. In 2011, Kundu and Bertino<ref name=":0" /> demonstrated some of the exploits on placement new. Some of the attacks are buffer overflow attacks, object overflow, selective stackguard overriding, virtual pointer subterfuge, memory misalignment attacks. In 2015, GCC released a patch<ref>{{Cite web|title=Martin Sebor - [PING] [PATCH] c++/67942 - diagnose placement new buffer overflow|url=https://gcc.gnu.org/legacy-ml/gcc-patches/2015-10/msg02001.html|access-date=2020-06-15|website=gcc.gnu.org}}</ref> based on the findings in
== Notes ==
Line 257 ⟶ 266:
== References ==
{{refbegin
* {{cite book
* {{cite book
* {{cite web
* {{cite book
* {{cite book
* {{cite book
* {{cite book
* {{cite web|ref=CITEREFMcCluskey2000|url=http://glenmccl.com/nd_cmp.htm|title=Placement New/Delete|publisher=Glen McCluskey &
* {{cite news
* {{cite book
* {{cite book
* {{cite book
* {{cite book
* {{cite book
* {{cite book
* {{cite web
{{refend}}
== Further reading ==
* {{cite book|last=Franek|first=Frantisek|title=Memory as a Programming Concept in C and C++|publisher=[[Cambridge University Press]]|isbn=978-0-521-52043-0|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|first=Marshall|last=Cline|
* {{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|year=2003|publisher=[[IBM]]|
* {{cite web|url=http://msdn.microsoft.com/en-us/library/t48aek43.aspx|work=[[MSDN]]|title=The operator new Function|publisher=[[Microsoft]]|access-date
* {{cite web|url=http://msdn.microsoft.com/en-us/library/kewsb8ba.aspx|work=[[MSDN]]|title=new Operator (C++)|publisher=[[Microsoft]]|access-date
{{DEFAULTSORT:Placement Syntax}}
[[Category:Articles with example C++ code]]
[[Category:C++]]
|