title: Trying to free reallocated memory gives free(): invalid pointer
body: <p>I can't figure out why I am getting the error <code>free(): invalid pointer</code> when running this code. I thought <code>p</code> is going to point to a block of memory set aside for the structures, so I am trying to free pointers beginning from the <code>p</code> e.g. <code>p+1</code>, but this is no good. What am I missing here?</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct s {
      int x;
      
  };
  int main()
  {
      struct s *p = NULL;
      for (int i = 0; i &lt; 3; i++) {
          if ((p = realloc(p, (i+1) * sizeof(struct s))) != NULL) {
              struct s x = {.x=i*10};
              *(p+i) = x;
              
          } else exit(EXIT_FAILURE);
      }
      for (int i=0;i&lt;3;i++) {printf(&quot;%d &quot;, (p+i)-&gt;x);}
      //free(p);
      free(p+1);
      //free(p+2);
      return 0;
  }
  </code></pre>