Placement New and directions
<p>I have been trying to understand the Placement new concept. I searched on the internet for some examples. And with all that info I created one example myself, here´s the code:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;new&gt;

using namespace std;

class MyClass
{
 private:
         size_t num;
 public:
        ~MyClass(void) { cout&lt;&lt;"DESTRUCTOR"&lt;&lt;endl; };
        void print(void) { cout&lt;&lt;num&lt;&lt;endl; };
        void set(size_t num) { this-&gt;num = num; };
};

int main(void)
{

/** Just a little test */

 MyClass* myClass = new MyClass;
 myClass-&gt;set(32);
 myClass-&gt;print();

 delete myClass;

 /***/ 


 //Placement new code sample.

 char* memAlloc = new char[sizeof(MyClass)];

 cout&lt;&lt;"memAlloc : "&lt;&lt;&amp;memAlloc&lt;&lt;endl;

 MyClass* ptr = new(memAlloc) MyClass;

 ptr-&gt;set(95);
 ptr-&gt;print();

 cout&lt;&lt;"ptr : "&lt;&lt;ptr&lt;&lt;endl;

 ptr-&gt;~MyClass();
 delete[] memAlloc;

 return 0;
}
</code></pre>

<p>As I understand ptr will use the memory allocated by memAlloc with placement new. In this case, memAlloc and ptr will have the same address in memory? I tried to print it (a you sure see) but get different results. Using Code Blocks IDE with gcc 4.4.1 :</p>

<pre><code>memAlloc : 0x22ff04
ptr      : 0x2e1038
</code></pre>

<p>Can this be possible?, Am I printing it the wrong way?</p>

<p>Thanks a lot for any advice.</p>
