The placement new operator can specify memory locations when allocating memory. The following program uses the placement new operator and the general new operator to allocate memory to the object.
//Placenew.cpp--new, placement new, no delete#include <iostream>#include<string>#include<New>using namespacestd;Const intBUF = +;classjusttesting{Private: stringwords; intNumber ; Public: Justtesting (Const string&s ="Just Testing",intn =0) {words= S; Number = n; cout << Words <<"constructed\n"; } ~justtesting () {cout << words <<"destroyed\n"; } voidShow ()Const{cout << words <<", "<< number <<Endl;}};intMainvoid){ Char*buffer =New Char[BUF];//get a block of memoryJusttesting *PC1, *PC2; PC1=New(buffer) justtesting;//Place object in bufferPC2 =NewJusttesting ("Heap1", -);//Place object on heapcout<<"Memory block address:\n"<<"Buffer:"<< (void*) Buffer <<"Heap:"<< 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=NewJusttesting ("HEAP2",Ten); cout<<"Memory contents: \ n"; cout<< PC3 <<": "; PC3-Show (); cout<< PC4 <<": "; PC4-Show (); Delete PC2; //Free heap1Delete PC4;//Free heap2delete [] buffer;//Free buffercout <<"done\n"; return 0;}
Execution Result:
new Just testing CONSTRUCTEDHEAP1 constructedmemory block Address:buffer: Heap: 0x936a248 memory Contents: 0x936a008 : Just testing, 0 0x936a248 : Heap1, 20 bad idea constructedHeap2 constructedmemory contents: 0x936a008 : Bad Idea, 6 0x936a290 : Heap2, 10 heap1 destroyedHeap2 destroyeddone
The above program has two problems with the placement new operation. First, when you create a second object, the placement new operator uses a new object to overwrite the memory unit used for the first object. Obviously, if a class dynamically allocates memory for its members, this raises the issue.
Second, when you use Delete for PC2 and PC4, the destructor is called automatically for PC2 and PC4 objects that are pointed to, but when you use delete[] for buffer, destructors are not called for objects created using the layout new operator.
To determine that two units do not overlap, you can do this:
Newnewsizeof(justtesting) justtesting ("Betteridea" 6);
Where pointer pc3 is the size of the Justtesting object relative to the PC1 offset
The second lesson is that if you use the placement new operator to allocate memory for an object, you must ensure that its destructor is called, but how do you ensure it?
For example, an object created in a heap can do this:
Delete PC2;
However, for objects created using the placement new operator, you cannot call delete as follows
Delete PC1; NO!!!
The reason for this is that the delete can be used in conjunction with the regular new operator, but not with the placement new operator.
So we're going to show the call destructor, you must specify the object to destroy:
Pc3->~justtesting (); Destroy object pointed to by PC3
intMainvoid){ Char*buffer =New Char[BUF];//get a block of memoryJusttesting *PC1, *PC2; PC1=New(buffer) justtesting;//Place object in bufferPC2 =NewJusttesting ("Heap1", -);//Place object on heapcout<<"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 ("betteridea", C8>6 ); PC4=NewJusttesting ("HEAP2",Ten); cout<<"Memory Contents:"; cout<< PC3 <<": "; PC3-Show (); cout<< PC4 <<": "; PC4-Show (); Delete PC2; //Free heap1Delete PC4;//Free heap2//explicitly destroy placement new objectpc3->~justtesting (); // Destroy object pointed to by PC3 pc1->~justtesting (); // Destroy object pointed to by PC1delete []buffer;//Free buffercout <<"done/n"; return 0;}
Placement new operator