title: realloc() invalid next size
body: <p>Any idea why I'm getting the error, realloc(): invalid next size ?</p>
  
  <pre><code>  while (loc) {
      char nextLine[MAX_PATH_LEN + 30];
      sprintf(nextLine, "&lt;p&gt;%s:%d&lt;/p&gt;", loc-&gt;item.pathname,
      loc-&gt;item.offset);
      DPRINTF('h', ("Next line is: %s, length: %d\n", nextLine, strlen(nextLine))\
  );
  
      DPRINTF('h', ("spaceUsedUp is %d\n", spaceUsedUp));
      while (spaceUsedUp + strlen(nextLine) &gt; allocatedSize) {
        printf("Allocated size is now %d\n", allocatedSize);
        allocatedSize *= 2;
      }
  
      DPRINTF('h', ("Will realloc size %d\n", allocatedSize));
      char *tmp = (char *)realloc(result, allocatedSize);
      DPRINTF('h', ("Done with the realloc.\n"));
      fflush(stdout);
      if (tmp == NULL) {
        perror("realloc");
      }
      result = tmp;
      tmp = NULL;
  
      int theOne = (spaceUsedUp == 0) ? 0 : 1;
      memcpy(result+spaceUsedUp-theOne, nextLine, sizeof(nextLine));
  
      spaceUsedUp += strlen(nextLine) - theOne;
      loc = loc-&gt;nextLocation;
    }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Next line is: &lt;p&gt;/dirA/f3:6162&lt;/p&gt;, length: 20
  spaceUsedUp is 0
  Will realloc size 100
  Done with the realloc.
  Next line is: &lt;p&gt;/dirA/f3:6038&lt;/p&gt;, length: 20
  spaceUsedUp is 19
  Will realloc size 100
  *** glibc detected *** ./proj3/disksearch: realloc(): invalid next size: 0x0000000001797fa0 ***
  </code></pre>