title: How do malloc() and free() work?
body: <p>I want to know how <code>malloc</code> and <code>free</code> work.</p>
  
  <pre><code>int main() {
      unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char));
      memset(p,0,4);
      strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes**
      cout &lt;&lt; p;
      free(p); // Obvious Crash, but I need how it works and why crash.
      cout &lt;&lt; p;
      return 0;
  }
  </code></pre>
  
  <p>I would be really grateful if the answer is in depth at memory level, if it's possible.</p>