title: how placement new works in c++?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/222557/cs-placement-new">C++&#39;s &ldquo;placement new&rdquo;</a>  </p>
  </blockquote>
  
  
  
  <p>I just learned about the placement new operator and tried creating my own memory manager.<br>
  Here is the code of my template base class for Memory  </p>
  
  <pre><code>#ifndef _MEMORY_BASE_H_  
  #define _MEMORY_BASE_H_  
  //=========================================!  
  #include &lt;new&gt;  
  #include &lt;exception&gt;  
  #include &lt;iostream&gt;  
  //=========================================!  
  using namespace std;  
  //=========================================!  
  template &lt;typename T&gt;
  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 &lt;typename T&gt;
  void* Memory_Base&lt;T&gt;::operator new(size_t T)
  {
  cout &lt;&lt; "Memory_Base&lt;T&gt;:: new called" &lt;&lt; endl;
  void* pTemp = malloc(sizeof(T));
  if(!pTemp)
      throw bad_alloc();
  else 
      return pTemp;
  }
  //=========================================!  
  template &lt;typename T&gt;
  void* Memory_Base&lt;T&gt;::operator new(size_t T,void* pAddress)
  {
  cout &lt;&lt; "Memory_Base&lt;T&gt;:: placement new called" &lt;&lt; endl;
  return pAddress;
  }
  //=========================================!
  template &lt;typename T&gt;
  void Memory_Base&lt;T&gt;::operator delete(void* pAny)
  {
  cout &lt;&lt; "Memory_Base&lt;T&gt;:: delete called" &lt;&lt; endl;
  free(pAny);
  }
  //=========================================!
  #endif
  </code></pre>
  
  <p>Now, i inherited myclass from above class,in another header file , something like as below  </p>
  
  <pre><code>#ifndef _MY_CLASS_H_
  #define _MY_CLASS_H_
  //=========================!
  #include "Memory_Base.h"
  //=========================!
  class MyClass :public Memory_Base&lt;MyClass&gt;
  {
   private :
  int ma;
   public :
  MyClass():ma(-1){}
  ~MyClass(){}
  };  
  //============================!
  #endif  
  </code></pre>
  
  <p>Now, in my main, i am trying to create objects of myclass in the following manner </p>
  
  <pre><code>//============================!
  #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(&amp;obj)MyClass();
  return 0;
  }
  //============================!
  </code></pre>
  
  <p>Questions ::<br>
  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 ?  </p>
  
  <ol start="2">
  <li>my obj[10], is in stack, but, the destructor is not getting called.  </li>
  </ol>
  
  <p>Any ideas ?  </p>
  
  <p>Atul  </p>
  
  <p>P.S :: I have to implement the new[] and delete[] in Memory_Base.  </p>