Sunday, April 20, 2014

C++ - Placement New

#include <cstring>
#include <iostream>
using namespace std;

// Class used within this example
class Place{
     char* name;

          Place(const char* sz){
               name = new char[strlen(sz)+1];
               strcpy(name, sz);



               delete [] name;
          char* getName(){

               return name;



int main(){

     /* Create a Place pointer pointing to space allocated in memory.
        Space is allocated for 5 Place objects with "operator new".
        Once space is reserved it must be cast into ptr1 as a Place pointer */

     Place* ptr1 = (Place*) (operator new (sizeof(Place)*5));

     /* Create another Place pointer pointing to same location as ptr1.
        This is used to iterate through the memory to create the 5 objects. */

     Place* ptr2 = ptr1;

     /* Place pointer to store address of objects to be deleted.
        (Alternately, ptr2 can also be used for this purpose) */

     Place* tempPtr;

     /* After each loop ptr2 is incremented.
        This moves the pointer up by amount of memory required for one Place object */

     for(int i = 0; i < 5; i++, ptr2++){

          /* Creates the new Place object at location where ptr2 is pointing. */

          new (ptr2) Place("hello");          


     /* Saves the address of the object to be deleted.
        ptr1 is used because ptr2 is pointing past last object due to the previous for loop. */    

     tempPtr = &(ptr1[2]);
     /* Calls the destructor for the selected object. */


     /* Creates new Place object at same location as previously destructed object.
        (destructed object's address was saved in tempPtr) */
     new (tempPtr) Place("First new place");

     // Repeat for any other objects    

     tempPtr = &(ptr1[4]);


     new (tempPtr) Place("Second new place");

     // This is just a check to make sure it worked

     for(int i = 0; i < 5; i++){

          cout << ptr1[i].getName() << endl;


     return 0;


No comments:

Post a Comment