Positioning in C + + placement new (placement new)

Source: Internet
Author: User

In general, when you use new to request space, you allocate space from the heap of the system. The location of the requested space is determined by the actual use of the memory at that time. However, in some special cases, it may be necessary to create an object in the specific memory specified by the programmer, which is called the "placement new" operation.

The syntax of the position placement new operation differs from the normal new operation. For example, the following statement is generally used as a * p=new A; the application space, while the positioning of the new operation uses the following statement * P=new (PTR) A; application space, where PTR is the programmer's designated memory header address. Review the following procedures.

#include <iostream>using namespace std;class a{    int num;public:    A () {        cout<< "a ' s constructor" <<endl;    }    ~a () {        cout<< "~a" <<endl;    }    void Show () {        cout<< "num:" <<num<<endl;    }; int main () {    char mem[100];    mem[0]= ' A ';    mem[1]= ' + ';    mem[2]= ' + ';    mem[3]= ' + ';    cout<< (void*) mem<<endl;    A * P=new (MEM) A;    cout<<p<<endl;    P->show ();    P->~a ();    GetChar ();}

  

Program Run Result:
0024f924
A ' s constructor
0024f924
Num:65
~a

Read the above procedure and pay attention to the following points.
(1) Positioning the new operation, you can either build the object on the stack or build the object on the heap (heap). In this case, an object is generated on the stack.

(2) using the statement A * P=NEW (MEM) A; When locating the generated object, the pointer p and the group name Mem Point to the same piece of storage. So, instead of positioning the new operation is to apply for space, rather than to take advantage of the already requested space, the real application space work is done before this.

(3) Using the statement a *p=new (MEM) A; Locating the generated object is a constructor that automatically calls class A, but because the object's space is not automatically freed (the object is actually borrowing someone else's space), the destructor for the calling class must be displayed, such as P->~a () in this example.

(4) Use placement new only as a last resort, only if you really care about the object's specific location in memory. For example, your hardware has a memory image of the I/O timer device, and you want to place a clock object in which location.

Positioning in C + + placement new (placement new)

Related Article

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.