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 an object.
Placenew.cpp--New, placement new, no delete #include <iostream> #include <string> #include <new> us ing namespace std; const int BUF = 512; Class Justtesting {private:string words int number; public:justtesting (const string &s = "Just testing", int n = 0 {words = s; number = n; cout << words << "constructed/n";} ~justtesting () {cout << words << "destroyed/n"; void Show () const {cout << words << "," << number << Endl;}}; int main (void) {char *buffer = new char [BUF];//Get a block of memory justtesting *PC1, *PC2; pc1 = new (buffer) justtes Ting Place object in Buffer PC2 = new Justtesting ("Heap1", 20); Place object on heap cout << "Memory block address:/n" << "Buffer:" << (void *) buffer << Hea P: "<< pc2 << Endl; cout << "Memory contents:/n"; cout << pc1 << ":"; Pc1->show (); cout << pc2 << ":"; Pc2->show (); JustteSting *PC3, *PC4; PC3 = new (buffer) justtesting ("Bad Idea", 6); PC4 = new Justtesting ("Heap2", 10); cout << "Memory contents:/n"; cout << pc3 << ":"; Pc3->show (); cout << pc4 << ":"; Pc4->show (); Delete PC2; Free HEAP1 Delete pc4; Free HEAP2 Delete [] buffer; Free buffer cout << "done/n"; return 0; }
The program creates a 512-byte memory buffer using the new operator, creates two Justtesting objects in the heap using the new operator, and attempts to create two Justtesting objects in the memory buffer using the layout new operator.
Here is the output of my Computer
< slightly >
The above program has two problems with the layout new operation. First, when you create the second object, the layout new operator uses a new object to overwrite the memory unit for the first object. Obviously, this raises a problem if the class dynamically allocates memory for its members.
Second, when you use Delete for PC2 and PC4, calls to destructors are automatically invoked for PC2 and PC4 objects; However, when you use delete[for buffer, the destructor is not invoked for objects created using the layout new operator.
To determine that two units do not overlap, you can do this:
PC1 = new (buffer) justtesting; PC3 = new (buffer + sizeof (justtesting)) justtesting ("Better idea", 6);
Where the pointer pc3 the offset from the PC1 to the size of the Justtesting object
The second lesson is that if you use the layout new operator to allocate memory to an object, you must ensure that its destructor is invoked, but how to ensure it.
For example, an object created in the heap can do this:
Delete PC2;
However, for objects created using the layout new operator, you cannot call delete as follows
Delete PC1; NO!!!
The reason is that delete can be used in conjunction with the regular new operator, but not with the layout new operator.
So we're going to show the calling destructor, you must specify the object to destroy:
Pc3->~justtesting (); Destroy object pointed to by PC3
int main (void) {char *buffer = new CHAR[BUF];//Get a block of memory justtesting *PC1, *PC2; pc1 = new (buffer) justtes Ting Place object in Buffer PC2 = new Justtesting ("Heap1", 20); Place object on heap cout << ' Memory block addresses:/n ' << ' buffer: ' << (void *) buffer << Heap: "<< pc2 << Endl; cout << "Memory contents:"; cout << pc1 << ":"; Pc1->show (); cout << pc2 << ":"; Pc2->show (); Justtesting *PC3, *PC4; Fix placement New Location PC3 = new (buffer + sizeof (justtesting)) justtesting ("Better Idea", 6); PC4 = new Justtesting ("Heap2", 10); cout << "Memory contents:"; cout << pc3 << ":"; Pc3->show (); cout << pc4 << ":"; Pc4->show (); Delete PC2; Free HEAP1 Delete pc4; Free HEAP2//explicitly destroy placement new object pc3->~justtesting (); Destroy object pointed to by PC3 pc1->~justtesting (); Destroy object pointed to by PC1 Delete []Buffer Free buffer cout << "done/n"; return 0; }