title: c++ malloc and free object creation and deletion
body: <p>I will first post my test program in C++ :</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class X
  {
       int x;
       public:
       X()
       {
         cout&lt;&lt;"constructing\n";
         x=0;
       }
       int&amp; getx()
       {
         return x;
       }
       ~X()
       {
         cout&lt;&lt;"destroying\n";
       }
  };
  int main()
  {
       X* p=(X*)malloc(sizeof(X));
       ++p-&gt;getx();
       p-&gt;getx()*=5;
       cout&lt;&lt;p-&gt;getx();
       free(p);
       return 0;
  }
  </code></pre>
  
  <p>Output :</p>
  
  <pre><code>5
  </code></pre>
  
  <p>Now before anyone complains why i used <code>malloc</code> &amp; <code>free</code> in a C++ program, I would like to reiterate that it is just a test program and the above could have been done even with <code>operator new</code> &amp; <code>operator delete</code>. But my questions still remain the same that are:</p>
  
  <ol>
  <li>Even though no object of X is created using <code>malloc or operator new</code> how can we access the class X's variable x ?</li>
  <li>Clearly <code>free &amp; operator delete</code> also do not destroy the objects and perform mere dallocations. What would happen if I create an object with <code>new</code> but use <code>operator delete or free</code> instead of <code>delete</code> ? Would my object still be there &amp; will it be still usable ?</li>
  </ol>