0

Possible Duplicate:
C++'s “placement new”

I just learned about the placement new operator and tried creating my own memory manager.
Here is the code of my template base class for Memory

#ifndef _MEMORY_BASE_H_  
#define _MEMORY_BASE_H_  
//=========================================!  
#include <new>  
#include <exception>  
#include <iostream>  
//=========================================!  
using namespace std;  
//=========================================!  
template <typename T>
class Memory_Base  
{  

public :  
//standard new
void* operator new(size_t T);  

// placement new  
void* operator new(size_t T,void* pAddress);  

//standard delete
void operator delete(void* pAny);
};  
//=========================================!
// Implementation 
//=========================================! 
template <typename T>
void* Memory_Base<T>::operator new(size_t T)
{
cout << "Memory_Base<T>:: new called" << endl;
void* pTemp = malloc(sizeof(T));
if(!pTemp)
    throw bad_alloc();
else 
    return pTemp;
}
//=========================================!  
template <typename T>
void* Memory_Base<T>::operator new(size_t T,void* pAddress)
{
cout << "Memory_Base<T>:: placement new called" << endl;
return pAddress;
}
//=========================================!
template <typename T>
void Memory_Base<T>::operator delete(void* pAny)
{
cout << "Memory_Base<T>:: delete called" << endl;
free(pAny);
}
//=========================================!
#endif

Now, i inherited myclass from above class,in another header file , something like as below

#ifndef _MY_CLASS_H_
#define _MY_CLASS_H_
//=========================!
#include "Memory_Base.h"
//=========================!
class MyClass :public Memory_Base<MyClass>
{
 private :
int ma;
 public :
MyClass():ma(-1){}
~MyClass(){}
};  
//============================!
#endif  

Now, in my main, i am trying to create objects of myclass in the following manner

//============================!
#include "MyClass.h"
//============================!
int main()
{
// This is how new for MyClass is called
MyClass* pMyClass = new MyClass();

// This is how placement new for MyClass is called
MyClass obj[10];
MyClass* pMyClass1 = new(&obj)MyClass();
return 0;
}
//============================!

Questions ::
1. When i run main, the base address of obj and pMyClass1 were the same, as expected. However, i am just returning the pointer pAddress, then how the placement new works ?

  1. my obj[10], is in stack, but, the destructor is not getting called.

Any ideas ?

Atul

P.S :: I have to implement the new[] and delete[] in Memory_Base.

Community
  • 1
  • 1
Atul
  • 745
  • 3
  • 9
  • 17

1 Answers1

1
MyClass obj[10]; 

Allocates and creates 10 objects of the type MyClass on the local storage.
Further,

MyClass* pMyClass1 = new(&obj)MyClass(); 

Just calls the constructor MyClass::MyClass(). The this pointer in the MyClass constructor will be equal to &obj. The returned pointer pMyClass1 will therefore be equal to &obj.

Also, In case of placement new it is your responsibility to destroy the placed object by explicitly calling the destructor, the placed objects destructor wont be called implcitly.

Since, the array of objects since placed on stack will be deallocated once the program returns in your case.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • @ the obj[10] thing, when it goes out of scope, shouldn't the destructor be called ? When i put a break point, in the destructor of MyClass, it never hits it – Atul Mar 20 '12 at 10:56
  • @Atul: It should. It will, put some traces statements & confirm. – Alok Save Mar 20 '12 at 11:13