title: How C++ placement new works?
body: <p>This question is to confirm I understood the concept right and take expert opinion on the style of usages and possible optimization.</p>
  
  <p>I am trying to understand "placement new" and following is the program I came up with...</p>
  
  <pre><code> #include &lt;iostream&gt;
   #include &lt;new&gt;
  
   class A {
   int *_a;
   public:
   A(int v) {std::cout&lt;&lt;"A c'tor clalled\n";_a= new int(v);}
   ~A() {std::cout&lt;&lt;"A d'tor clalled\n"; delete(_a);}
   void testFunction() {std::cout&lt;&lt;"I am a test function &amp;_a = "&lt;&lt;_a&lt;&lt;" a = "&lt;&lt;*_a&lt;&lt;"\n";}
  };
  int main()
  {
      A *obj1 = new A(21);
      std::cout&lt;&lt;"Object allocated at "&lt;&lt;obj1&lt;&lt;std::endl;
      obj1-&gt;~A();
      std::cout&lt;&lt;"Object allocated at "&lt;&lt;obj1&lt;&lt;std::endl;
      obj1-&gt;testFunction();
      A *obj2 = new(obj1) A(22);
      obj1-&gt;testFunction();
      obj2-&gt;testFunction();
      delete(obj1);// Is it really needed now? Here it will delete both objects.. so this is not the right place.
      //obj1-&gt;testFunction();
      //obj2-&gt;testFunction();
      return 0;
  }
  </code></pre>
  
  <p>When I run this program I get following o/p</p>
  
  <pre><code>A c'tor clalled
  Object allocated at 0x7f83eb404c30
  A d'tor clalled
  Object allocated at 0x7f83eb404c30
  I am a test function &amp;_a = 0x7f83eb404c40 a = 21
  A c'tor clalled
  I am a test function &amp;_a = 0x7f83eb404c40 a = 22
  I am a test function &amp;_a = 0x7f83eb404c40 a = 22
  A d'tor clalled
  I am a test function &amp;_a = 0x7f83eb404c40 a = 0
  I am a test function &amp;_a = 0x7f83eb404c40 a = 0
  </code></pre>
  
  <p>I have the following question...</p>
  
  <ul>
  <li>Is it a correct example to demonstrate placement new?</li>
  <li>member <em>a</em>  is dynamically allocated (with no placement new).  So why it is getting the same address for obj1 &amp; obj2. Is it just a coincidence? </li>
  <li>is D'tor call on line 15 a good practice? </li>
  </ul>
  
  <p>Please also point out of you see anything which I can improve on or just do not try. Any good reference or reads are also welcome.</p>