On new and delete (2)

Source: Internet
Author: User

The previous section describes the general use of the new operator, which we take together to look at the use of the locate new operator.

1. The position new operator is allocated two times in the allocated memory space. For example:
Char *buffer = new char[512];
Point *p = new (buffer) point (); Point is the class name
Point *q = new (buffer + sizeof) point ();
That is: Create the object P, q in the memory buffer Buffe

2. Delete is used in conjunction with the regular new operator, but cannot be used with the locate new operator and can only be called by objects created with the locate new operator, for example:
P->~point ();

3. Objects created using the locate new operator should be deleted in the reverse order of creation, because objects created late may depend on objects that were created earlier. Buffers used to store these objects can be freed only after all objects have been destroyed

Let's take a look at a specific example (refer to C + + primer Plus):
-------------------------------------------------------------
#include <iostream>
#include <string>
#include <new>

using namespace Std;
const int BUF = 512;

Class Test
{
Private
string words;
int number;
Public
Test (const string & s = "Test", int n = 0)
{
Words =s;
Number = n;
cout << words << "constructed\n";
}

~test ()
{
cout << words << "destroyed\n";
}

void Show ()
{
cout << words << "," << number << Endl;
}
};

int main ()
{
char * buffer = new char [BUF];
Test *p1, *P2;

P1 = new (buffer) Test;
P2 = new Test ("Heap1", 20);

cout << "Memory block addresses:\n"
<< "Buffer:"
<< (void*) buffer
<< "Heap:"
<< P2
<<endl;
cout << "Memory contents:\n";
cout << p1 << ":";
P1->show ();
cout << p2 << ":";
P2->show ();

Test *p3, *P4;
P3 = new (buffer + sizeof (test)) Test ("Place object in Buffer", 6);
P4 = new Test ("Heap2", 10);

cout << "Memory contents:\n";
cout << P3 << ":";
P3->show ();
cout << P4 << ":";
P4->show ();

Delete P2;
Delete P4;

P3->~test ();
P1->~test ();

delete [] buffer;
cout << "done\n";

return 0;
}
-------------------------------------------------------------
The results of the program run as follows:


We note that:
The program creates two test objects again in the memory buffer that was created. And in the final program, the destructor is called explicitly for objects created using the locate new operator, P1, P3. And the delete order is deleted in the reverse order of creation, because objects created later may depend on earlier objects. Finally, the release of the buffer must be carried out after P1 and P3 have been destroyed.
Another thing is to provide two different addresses in the buffer when creating P1 and P3 in the buffers, to avoid overlapping of two memory units. The P3 in the program specifies the size of the test object relative to the P1 offset.

On new and delete (2)

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.