title: How does free() know how much memory to deallocate?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1518711/c-programming-how-does-free-know-how-much-to-free">C programming : How does free know how much to free?</a>  </p>
  </blockquote>
  
  <p>When programming in C, I often use<code>malloc()</code> to allocate memory and <code>free()</code> to release it:</p>
  
  <pre><code>MyObject* objArr= (MyObject*) malloc(sizeof(MyObject)*numberOfObjects);
  /** Do stuff **/
  free(objArr);
  </code></pre>
  
  <p>How does <code>free()</code> know how much memory to deallocate? Does <code>malloc()</code> create a table somewhere to remember pointers and how much memory each pointer pointed to?</p>
  
  <p>If that is the case, will <code>free()</code> fail if I rename the pointer? e.g.:</p>
  
  <pre><code>MyObject* objArr= (MyObject*) malloc(sizeof(MyObject)*numberOfObjects);
  MyObject* newPtr= objArr;
  free(newPtr); /** Does this fail? **/
  </code></pre>
  
  <p>What will happen if I increment the pointer and then run <code>free()</code>? e.g.:</p>
  
  <pre><code>MyObject* objArr= (MyObject*) malloc(sizeof(MyObject)*numberOfObjects);
  newPtr++;
  free(newPtr); /** What happens now? **/
  </code></pre>
  
  <p>Will it deallocate an additional chunk of memory just off the end of the original array? </p>