This article describes the differences and linkages between the three types of new/delete.
New operator
The new operator (new operator) is an operator commonly used in our C + +, such as a * a = new A; Create a pointer to a object.
The new operator is divided into two steps:
1. Type-based size calls operator new to allocate a chunk of memory. If operator new is overloaded with a, call A::operator new (size_t), otherwise call global default C + + provided by itself:: operator new (size_t)
2. Construct the object.
operator NEW
The C + + default provides a operator new function, which is a common memory allocator that can allocate blocks of memory of any size.
For example T * tmp = (T*)(::operator new(size_t)size);
, create a memory block of size, and the pointer tmp points to this block of memory.
Operator delete should also be able to free blocks of memory of any size. operator delete wants to find out how much memory it is releasing, it has to know how much memory operator new allocated. There is a common way for operator new to tell operator what the memory size of the delete was allocated, that is, in the memory it returns in advance with some additional information to indicate the size of the allocated memory block. In other words, when you write the following statement,
Airplane *pa = new airplane;
You don't get a block of memory that looks like this:
pa——> airplane对象的内存
Instead, you get a block of memory like this:
pa——> 内存块大小数据 + airplane对象的内存
Rewrite OPERAOTR New
Operator New's job is to allocate memory, why rewrite: for efficiency. Some classes consume very little memory, and each time you create a memory that is too slow, you can allocate a large pool of memory and then construct the object directly in the memory pool each time.
For example, there is an airplane class:
... }; // 表示一个飞机对象 class airplane { public: ... private: airplanerep *rep; // 指向实际描述 };
A airplane object is not big, it contains only a pointer, and when you call operator new to allocate a airplane object, the resulting memory may be more than what is needed to store the pointer (or a pair of pointers). Why, as we described earlier, C + + provides default operator new allocated memory that requires the size of the memory to be recorded.
By overriding operator new, you can design a unique way of allocating memory. For example, when you create a airplane in http://www.kuqin.com/effectivec2e/ch02e.htm, you first create a large chunk of memory and use a linked list to control unused areas of memory. Each time new, select a node from the list as the allocated memory.
After rewriting operator new, when building an object with new operator, the operator new function is automatically called and the returned pointer is used as the assigned pointer.
You cannot delete the memory directly by using the Delete object that you created on the operator new allocated large memory.
The problem is that operator new (the one defined in airplane) returns a pointer to the memory that does not take the lead, and operator delete (the default one) assumes that the memory passed to it contains header information. This is the cause of the tragedy.
Placement NEW
Placement new constructs an object on the already allocated memory. For example new(p)T(value)
, use the constructor T (value) to construct a new T object on the first allocated memory address that P points to.
Discuss
There's nothing to say about new operator, which is the new operator we normally use, and he asks to allocate a chunk of memory and construct the object.
When we need to control the memory application mechanism, for example, we want to create a memory pool, and then construct the object on this block of memory, there are two ways:
1. Rewrite operator new
Examples of such http://www.kuqin.com/effectivec2e/ch02e.htm cited. After overriding the operator new for the class, the class constructs the object, automatically calling the overridden operator new to allocate memory.
2. Using Placement NEW
First request a piece of memory (you can use the default operator new), and then create the object on this block of memory.
T* tmp = (T*)(::operatornewsizeof(T))));//申请内存new (p)T1(value)//构造函数
New Operator/operator new and placement new