New/operator new/placement New in C + +

Source: Internet
Author: User

1. New/delete

New (and the corresponding delete) in C + + is the request and release of heap memory, and none of the two can be overloaded .

2. Operator New/operator Delete

If you want to implement different memory allocation behavior in C + +, you need to overload operator New,operator Delete. operator new AND operator + can be overloaded, but it is not possible to overload the prototype void* operator new (size_t size) Globally, typically only in classes (operator new) . Similarly, operator new[], operator delete, operator delete[] can also be overloaded,一般在重载了其中一个之后,最好将其他三个都重载一遍。

    Class myclass{...    .    };    myclass* p = new MyClass;

The new here actually performs three processes:
(1) Call operator new to allocate memory
(2) calling constructors to construct class objects
(3) Return pointer
operator new can be overloaded in a class, and if there is no overloaded operator new in the class, the global:: operator new is called to complete the allocation of the heap at the time of the new object.

3. Placement New

Placement New is an overloaded version of operator new, and placement new allows an object to be created in a well-allocated block of memory (heap or stack). in the prototype, Void*p actually points to the first address of an already allocated memory buffer.
The new operator allocates memory to find sufficient space in the heap, which is slow, and it is possible for an exception to fail to allocate memory. The placement new construct object is in a pre-prepared memory buffer, does not need to find memory, memory allocation time is constant, and does not appear in the middle of the program run out of memory exceptions. Therefore,placement new is suitable for applications that do not want to be interrupted for long periods of time.

How to use placement NEW:
(1) Buffer advance allocation
You can use heap space, or you can use stack space.

    Class myclass{...    };    char* buf = new Char[n*sizeof (MyClass) + sizeof (int)];    or    char buf[n*sizeof (MyClass) + sizeof (int)];

(2) Construction of objects

myclass* PClass = new (BUF) MyClass; Placement NEW

(3) Destruction of objects
Once an object has been used, the destructor must be explicitly called to destroy the object. However, the memory space is not freed so that other objects are constructed.

Pclass->~myclass (); To explicitly call a destructor

(4) Release of memory
If the buffer is in the heap, call delete[] buf to release it, and if it is in the stack, the memory is automatically freed when it jumps out of scope.

    Although the built-in type has no constructors, you can also use placement new    int* i = (int*) malloc (sizeof (int));    New (i) int ();

Note
In the C + + standard, for placement operator new [] like the following description: placement operator new[] needs implementation-defined amount of additional stor Age to save a size of array. So we have to apply more than the size of the original object, sizeof (int) bytes to hold the number of objects, or the size of the array.

New/operator new/placement New in C + +

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.