title: Why isn't realloc shrinking the array?
body: <p>I have trouble reducing the size of dynamically created array. Here's what my <code>main</code> function looks like:</p>
  
  <pre><code>int main(void) {
      // Intialize big array
      int * a = (int *)malloc(10*sizeof(int));
      assert(a);
      // Fill it with squares
      for (int i = 0; i &lt; 10; ++i)
          a[i] = i*i;
      // Expected to print 64
      printf("%d\n", a[8]);
      // Shrink the big array
      int * b = (int *)realloc(a, 5*sizeof(int));
      assert(b);
      // Expected to cause SEGFAULT
      printf("%d\n", b[8]);
      return 0;
  }
  </code></pre>
  
  <p>Everything works fine except for <code>printf("%d\n", b[8]);</code> line, as it prints <code>64</code>, but not causing SEGFAULT error as I expected it to. Why?</p>
  
  <p>I think I missing something simple, because I've seen lots of SO questions related to shrinking the memory with <code>realloc</code>, but all of them say that it's possible.</p>
  
  <p>I'm using Ubuntu 14.04 with GCC 4.8.2 and compiling it with <code>-std=c99</code> option.</p>