Layout New operator

Source: Internet
Author: User

The new operator can specify the memory location when allocating memory. The following program uses the Layout New operator and the general new operator to allocate memory to the object.

// Placenew. CPP -- New, placement new, no Delete <br/> # include <iostream> <br/> # include <string> <br/> # include <New> </P> <p> using namespace STD; <br/> const int Buf = 512; </P> <p> class justtesting <br/>{< br/> PRIVATE: <br/> string words; <br/> int number; <br/> Public: <br/> justtesting (const string & s = "just testing", int n = 0) <br/> {<br/> words = s; number = N; cout <words <"constructed/N"; <br/>} <Br/> ~ Justtesting () {cout <words <"destroyed/N" ;}< br/> void show () const {cout <words <", "<number <Endl ;}< br/>}; </P> <p> int main (void) <br/> {<br/> char * buffer = new char [Buf]; // get a block of memory <br/> justtesting * pC1, * PC2; </P> <p> pC1 = new (buffer) justtesting; // place object in buffer <br/> PC2 = new justtesting ("heap1", 20 ); // place object on heap </P> <p> cout <"memory block address:/N" <"buffer:" <br/> <(void *) buffer <"heap:" <PC2 <Endl; <br/> cout <"memory contents:/N "; <br/> cout <pC1 <":"; <br/> pC1-> show (); <br/> cout <PC2 <":"; <br/> PC2-> show (); </P> <p> justtesting * PC3, * pc4; <br/> PC3 = new (buffer) justtesting ("bad idea", 6); <br/> pc4 = new justtesting ("heap2", 10); </P> <p> cout <"memory contents: /n "; <br/> cout <PC3 <": "; <br/> PC3-> show (); <br/> cout <pc4 <":"; <br/> pc4-> show (); </P> <p> Delete PC2; // free heap1 <br/> Delete pc4; // free heap2 <br/> Delete [] buffer; // free buffer <br/> cout <"done/N"; </P> <p> return 0; <br/>}

This program creates a 512-byte memory buffer using the new operator, and then creates two justtesting objects in the heap using the new operator, and try to create two justtesting objects in the memory buffer using the Layout New operator.

Below is my computer's output

<Omitted>

 

There are two problems when the above program uses the layout new operation. First, when creating the second object, the layout new operator uses a new object to overwrite the memory unit used for the first object. Obviously, if the class dynamically allocates memory for its members, this will cause problems.

When Delete is used for PC2 and pc4, it is automatically called as the object directed to PC2 and pc4 to call the destructor. However, when Delete [] is used for buffer, the Destructor will not be called for objects created using the Layout New operator.

To ensure that two units do not overlap, you can do this:

PC1 = new (buffer) justtesting; <br/> PC3 = new (buffer + sizeof (justtesting) justtesting ("better idea", 6 );

The offset of pointer PC3 to pC1 is the size of the justtesting object.

 

The second lesson is that if the Layout New operator is used to allocate memory to an object, you must ensure that the Destructor is called. But how can this problem be ensured?

For example, you can create an object in the heap as follows:

Delete PC2;

However, objects created using the Layout New operator cannot call Delete as follows

Delete pC1; // No !!!

The reason is that the delete operation can be used with the regular New operator, but cannot be used with the layout new operator.

To call the destructor, you must specify the object to be destroyed:

PC3-> ~ Justtesting (); // destroy object pointed to by PC3

 

Int main (void) <br/>{< br/> char * buffer = new char [Buf]; // get a block of memory <br/> justtesting * pC1, * PC2; </P> <p> pC1 = new (buffer) justtesting; // place object in buffer <br/> PC2 = new justtesting ("heap1", 20 ); // place object on heap </P> <p> cout <"memory block addresses:/N" <"buffer:" <br/> <(void *) buffer <"heap:" <PC2 <Endl; <br/> cout <"memory contents:"; <br/> cout <pC1 <": "; <Br/> pC1-> show (); <br/> cout <PC2 <": "; <br/> PC2-> show (); </P> <p> justtesting * PC3, * pc4; <br/> // fix placement new location <br/> PC3 = new (buffer + sizeof (justtesting )) justtesting ("better idea", 6); <br/> pc4 = new justtesting ("heap2", 10); </P> <p> cout <"memory contents: "; <br/> cout <PC3 <": "; <br/> PC3-> show (); <br/> cout <pc4 <": "; <br/> pc4-> show (); </P> <p> Delete PC2; // free hea P1 <br/> Delete pc4; // free heap2 <br/> // explicitly destroy placement new object <br/> PC3-> ~ Justtesting (); // destroy object pointed to by PC3 <br/> pC1-> ~ Justtesting (); // destroy object pointed to by pC1 <br/> Delete [] buffer; // free buffer <br/> cout <"done/N "; </P> <p> return 0; <br/>}

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.