title: realloc: invalid next size; memcpy
body: <p>I have code like this:</p>
  
  <pre><code>char buf[128];
  char *data = NULL;
  char *temp = NULL;
  
  int full = 0;
  
  int n = 0;
  
  do {
       bzero(buf, sizeof(buf));
       n = recv(newsock, buf, sizeof(buf), 0);
  
       full += sizeof(buf);
  
       if(n != 0)
            temp = realloc(data, sizeof(buf));
  
       if(temp != NULL) {
            data = temp;
       } else {
           // error
       }
  
       memcpy(data + (full - sizeof(buf)), buf, sizeof(buf));
  
       printf("%s\n",data);
  } while(n &gt; 0);
  </code></pre>
  
  <p>In this code i try get some data from socket to buffer and put this data into the memory. But i have problem. On third iteration of while cycle i get message like this:</p>
  
  <pre><code>*** glibc detected *** ./server: realloc(): invalid next size: 0x09a4c008 ***
  </code></pre>
  
  <p>When i remove memcpy() function everything alright, but how can i put data into memory? What is wrong? </p>