Source: http://www.cnblogs.com/wanghetao/archive/2011/11/21/2257403.html
About Placement New
hzh512
1. Placement new meaning
placement new is a standard, global version of the overloaded operator new , It cannot be replaced by a custom version (unlike regular versions of operator new and operator delete can be replaced).
Void *operator new ( size_t, void *p ) throw () { return p; }
Placement new execution ignores the size_t parameter, returning only the second argument. The result is that the user is allowed to put an object in a specific place, reaching the effect of invoking the constructor.
Unlike other normal new, it has one more parameter in parentheses. For example:
widget * p = new widget; - - - - - - - - - //ordinary new
pi = new (PTR) int; pi = new (PTR) int; //placement new
The parameter ptr in parentheses is a pointer to a memory buffer, Placement new will allocate an object on this buffer. The return value of the placement new is the address of the constructed object (such as the pass parameter in parentheses). Placement new is primarily intended for applications where time is very high, because the time allotted for these programs is deterministic, a program that is long running without interruption, and a garbage collector (garbage collector )。
2. New, operator new and placement new differences
NEW: cannot be overloaded, its behavior is always consistent. It first calls operator new to allocate memory, and then calls the constructor to initialize that piece of memory.
Operator NEW: To implement different memory allocation behavior, operator new should be overloaded instead of new.
Delete and operator delete are similar.
Delete First invokes the object's destructor and then calls operator delete to release the memory used.
Placement NEW: Just one version of operator new overload. It does not allocate memory, but returns a pointer to a memory that has already been allocated. Therefore, you cannot delete it, but you need to call the object's destructor.
3. The new operator execution process
(1). Call operator new to allocate memory;
(2). Call the constructor to generate the class object;
(3). Returns the corresponding pointer.
Operator new is like operator+, which can be overloaded. If operator new is not overloaded in the class, then the call is global: operator new to complete the heap allocation. Similarly, operator new[], operator delete, operator delete[] can also be overloaded, in fact, operator new is also an overloaded version of operator new, but rarely used. If you want to create an object in an already allocated memory, it won't work when you use new. In other words, placement new allows you to construct a new object in an already allocated memory (stack or heap). The void*p in the prototype is actually the first address that points to an already allocated memory buffer.
4. Reasons for the existence of Placement new
(1). Using placement new to solve the problem with buffer
Problem Description: When you allocate an array buffer with new, the execution is inefficient because the default constructor is called. If there is no default constructor, a compile-time error occurs. If you want to create an object on pre-allocated memory, it won't work with the default new operator. To solve this problem, you can use the placement new construct. It allows you to construct a new object onto the pre-allocated memory.
(2). Increasing the time-space efficiency problem
allocating memory using the new operator requires finding enough free space in the heap, which is obviously slow, and there is the possibility of an exception (not enough space) to allocate memory.
Placement new can solve this problem. We construct objects in a pre-prepared memory buffer, do not need to find memory, memory allocation time is constant, and do not appear in the middle of the program run out of memory exceptions. Therefore, placement new is ideal for applications that do not want to be interrupted for long periods of time.
5. Steps to use
In many cases, the use of placement new differs from other common new. This provides the steps to use it.
First step cache advance allocation
There are three ways of doing this:
1. In order to ensure that the memory alignmen of the buffers used by placement new are properly prepared, the normal new is used to allocate it: allocate on the heap
Class Task;
char * buff = new [sizeof (Task)]; Allocating memory
(Note that auto or static memory is not all correctly arranged for each object type, so you will not be able to use them with placement new.) )
2. Allocating on the stack
Class Task;
Char buf[n*sizeof (Task)]; Allocating memory
3. There is also a way to use the address directly. (Must be a meaningful address)
void* buf = reinterpret_cast<void*> (0xf00f);
Second step: Assignment of objects
In the buffer just allocated, call placement new to construct an object.
Task *ptask = new (BUF) task
Step Three: Use
Use assigned objects in the normal way:
Ptask->memberfunction ();
Ptask-> member;
//...
Fourth step: object's destruction
Once you have finished using this object, you must call its destructor to destroy it. Call the destructor in the following way:
Ptask->~task (); Call an external destructor
Fifth Step: Release
You can reuse the cache and assign it a new object (Repeat step 2,3,4) If you're not going to use this cache again, you can release it like this:
delete [] buf;
Skipping any step can lead to run-time crashes, memory leaks, and other unexpected situations. If you do need to use placement new, follow these steps carefully.
Reprint-C + +-placement NEW