title: Can I assume that calling realloc with a smaller size will free the remainder?
body: <p>Let’s consider this very short snippet of code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  
  int main()
  {
      char* a = malloc(20000);
      char* b = realloc(a, 5);
  
      free(b);
      return 0;
  }
  </code></pre>
  
  <p>After reading the man page for realloc, I was not entirely sure that the second line would cause the 19995 extra bytes to be freed. To quote the man page: <code>The realloc() function changes the size of the memory block pointed to by ptr to size bytes.</code>, but from that definition, can I be sure the rest will be freed? </p>
  
  <p>I mean, the block pointed by <code>b</code> certainly contains 5 free bytes, so would it be enough for a lazy complying allocator to just not do anything for the realloc line?</p>
  
  <p>Note: The allocator I use seems to free the 19 995 extra bytes, as shown by valgrind when commenting out the <code>free(b)</code> line : </p>
  
  <pre><code>==4457== HEAP SUMMARY:
  ==4457==     in use at exit: 5 bytes in 1 blocks
  ==4457==   total heap usage: 2 allocs, 1 frees, 20,005 bytes allocated
  </code></pre>