title: Concatenation using strcat and realloc produce unexpected errors
body: <p>I have encountered so called cryptic <code>realloc</code> <code>invalid next size error</code> , I am using <code>gcc</code> on <code>linux</code> my code is   </p>
  
  <pre><code> #include&lt;stdio.h&gt;
   #include&lt;stdlib.h&gt;
   #include&lt;string.h&gt;
  
  int main()
  {
   int i;
   char *buf;
   char loc[120];
   buf = malloc(1);
   int size;
  
   for(i=0;i&lt;1920;i++)
    {
      sprintf(loc,"{Fill_next_token = my_next_token%d; Fill_next_token_id = my_next_token_id = my_next_token_id%d}",i,i);
      size = strlen(buf)+strlen(loc);
      printf("----%d\n",size);
  
      if(!realloc(buf,size))
      exit(1);
      strcat(buf,loc);
      }
    }
  </code></pre>
  
  <p>(mine might be duplicate question) <a href="https://stackoverflow.com/questions/2939091/realloc-invalid-next-size">here</a> the solution somewhere lies by avoiding <code>strcat</code> and to  use <code>memcpy</code> , But in my case I really want to concatenate the string . Above code works for good for such 920 iterations but in case 1920 <code>realloc</code> gives invalid new size error. Please help to find alternative of concatenating , looking forward to be a helpful question for lazy programmers like me .</p>