title: What if, memory allocated using malloc is deleted using delete rather than free
body: <p>I came across an issue which I could not resolve.</p>
  <p>My question is, if I used <code>malloc</code> to allocate memory and then memory block is delete using <code>delete</code>?
  The general thumb rule is</p>
  <blockquote>
  <p>If we allocate memory using <strong>malloc</strong>, it should be deleted using <strong>free</strong>.</p>
  <p>If we allocate memory using <strong>new</strong>, it should be deleted using <strong>delete</strong>.</p>
  </blockquote>
  <p>Now, in order to check what happens if we do the reverse, I wrote a small code.</p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cstdio&gt;
  #include&lt;cstdlib&gt;
  
  using namespace std;
  
  class A
  {
    int p=10;
  public:
    int lol() {
      return p;
    }
  };
  
  int main()
  {
    int *a = (int *)malloc(sizeof(int)*5);
    for(int i=0; i&lt;4; i++) {
      a[i] = i;
      cout &lt;&lt; a[i] &lt;&lt; endl;
    }
  
    delete(a);
    // Works fine till here..
  
    A b;
    cout &lt;&lt; b.lol() &lt;&lt; endl;
    free(b); // Causes error.     
  
    return 0;
  }
  </code></pre>
  <p>The error which I get is:</p>
  <blockquote>
  <p>error: cannot convert ‘A’ to ‘void*’ for argument ‘1’ to ‘void
  free(void*)’</p>
  </blockquote>
  <p>I am unable to understand why is this happening. Please explain.</p>