New and operator new, placement new

Source: Internet
Author: User

"Placement new "? What exactly is it? I also heard about it in recent days. It seems that I am still far away from C ++! Placement new is a standard and global version of operator new. It cannot be replaced by a custom version (unlike ordinary Operator
New and operator Delete can be replaced with the user-defined version ).

Its prototype is as follows:
Void * operator new (size_t, void * P) Throw () {return P ;}

First, we will distinguish several confusing keywords: new, operator new, and placement new.
We should have used the new and delete operators. They apply for and release the memory in the heap, and neither of them can be overloaded. To implement different memory allocation behaviors, You need to reload operator new instead of new and delete.

See the following code:
Class myclass {...};
Myclass * P = new myclass;

Here, new actually executes the following three processes:


1. Call operator new to allocate memory; 2. Call constructor to generate class objects; 3. Return the corresponding pointer.

Operator new can be reloaded just like operator +, but it cannot be reloaded globally for the prototype void operator new (size_t size). Generally, it can only be reloaded in classes. If operator new is not overloaded in the class, the global OPERATOR: operator new is called to complete heap allocation. Similarly, operator new [], operator delete, and operator Delete [] can also be reloaded. Generally, If you reload one of them, reload the other three.

Placement new is the focus of this article. In fact, it is only an overloaded version of operator new, but we seldom use it. If you want to create an object in the allocated memory, using new will not work. That is to say, placement new allows you to construct a new object in a allocated memory (stack or heap. In the prototype, void * P actually points to the first address of an allocated memory buffer.

We know that using the new operator to allocate memory requires finding enough space in the heap. This operation is very slow and there may be exceptions that cannot allocate memory (insufficient space ). Placement new can solve this problem. The constructed objects are all carried out in a pre-prepared memory buffer. The memory allocation time does not need to be searched. The memory allocation time is a constant, and there is no exception of insufficient memory during the program running. Therefore, placement new is very suitable for applications that require high time requirements and do not want to be interrupted for a long time.

The usage is as follows:
1. Buffer Allocation in advance
You can use the heap space or stack space. Therefore, there are two allocation methods:
Class myclass {...};
Char * Buf = new char [N * sizeof (myclass) + sizeof (INT)]; or char Buf [N * sizeof (myclass) + sizeof (INT)];

2. Object Construction
Myclass * Pclass = new (BUF) myclass;

3. Object destruction
Once this object is used up, you must explicitly call the class destructor to destroy the object. However, the memory space will not be released for the construction of other objects.

Pclass-> ~ Myclass ();

4. Memory release
If the buffer zone is in the heap, call Delete [] Buf; to release the memory. If the buffer zone is in the stack, the buffer zone is valid in its scope, and the buffer zone is automatically released.

Note:

  • In the C ++ standard, placement operator new [] is described as follows: Placement operator new [] needs implementation-defined amount of additional storage to save a size of array. therefore, we must apply for an extra sizeof (INT) bytes larger than the original object size to store the number of objects, or the size of an array.
  • The new method in step 2 is placement new. In fact, there is no memory application, but the constructor is called to return a pointer to the allocated memory, therefore, you do not need to call Delete to release space when the object is destroyed, but you must call the destructor to destroy the object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.