title: realloc says Invalid next size
body: <p>Here is my code which should dynamically allocate memory for storing strings:</p>
  
  <pre><code>#define _XOPEN_SOURCE 700
  
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
      char **path = NULL;
  
      path = (char **) malloc(sizeof(char *));
  
      for (int i = 0; i &lt; 5; i++)
      {
              path[i] = strdup("TEST");
              path = (char **) realloc(path, sizeof(char *));
      }
  
      for (int i = 0; i &lt; 5; i++)
      {
              printf("%s\n", path[i]);
      }
  
      return 0;
  }
  </code></pre>
  
  <p>The above code breaks on the line where i have reallocated the memory.</p>
  
  <p>According to me the the memory allocated to path on first malloc is as given below:</p>
  
  <pre><code>path -----&gt; |   char *  | // path points to a character pointer which       inturn is pointing to nothing(Dangling).
  </code></pre>
  
  <p>So, at this point of time when the program ran the line:</p>
  
  <pre><code>path = (char **) malloc(sizeof(char *));
  </code></pre>
  
  <p>path[0] is currently in bounds and we can store starting address of one and only one string at path[0] and path[1] should be out of bounds at this point of time.</p>
  
  <p>So, when the very first time we entered the for loop where we are storing the address of string at path[i] we will be able to that as:</p>
  
  <pre><code>path[0] = strdup("TEST"); // is in bounds.
  </code></pre>
  
  <p>To store another string we need some more memory that path should be pointing.
  So i did realloc in the line just below as:</p>
  
  <pre><code>path = (char **) realloc(path, (char *));
  </code></pre>
  
  <p>So, according to me path should look like this in memory as below:</p>
  
  <pre><code>path ---------&gt;|old char *|-----&gt;"TEST"
    |-----------&gt;|new char *|-----&gt;(Dangling) // Pointing Nowhere.
  </code></pre>
  
  <p>So, now path[1] is also in bounds and we should be able to use that memory location. So i can't figure out why i am getting this Segmentation fault when i run my code:</p>
  
  <pre><code>*** glibc detected *** ./dynamic: realloc(): invalid next size: 0x0000000000602010 ***
  ======= Backtrace: =========
  /lib64/libc.so.6(+0x75018)[0x7f743153e018]
  /lib64/libc.so.6(+0x7ae96)[0x7f7431543e96]
  /lib64/libc.so.6(realloc+0xfa)[0x7f74315441aa]
  ./dynamic[0x40067f]
  /lib64/libc.so.6(__libc_start_main+0xe6)[0x7f74314e7bc6]
  ./dynamic[0x400569]
  ======= Memory map: ========
  00400000-00401000 r-xp 00000000 00:27 14459340                           /home/xpansat/c/basic/dynamic
  00600000-00601000 r--p 00000000 00:27 14459340                           /home/xpansat/c/basic/dynamic
  00601000-00602000 rw-p 00001000 00:27 14459340                           /home/xpansat/c/basic/dynamic
  00602000-00623000 rw-p 00000000 00:00 0                                  [heap]
  7f742c000000-7f742c021000 rw-p 00000000 00:00 0 
  7f742c021000-7f7430000000 ---p 00000000 00:00 0 
  7f74312b2000-7f74312c8000 r-xp 00000000 fd:01 173                        /lib64/libgcc_s.so.1
  7f74312c8000-7f74314c7000 ---p 00016000 fd:01 173                        /lib64/libgcc_s.so.1
  7f74314c7000-7f74314c8000 r--p 00015000 fd:01 173                        /lib64/libgcc_s.so.1
  7f74314c8000-7f74314c9000 rw-p 00016000 fd:01 173                        /lib64/libgcc_s.so.1
  7f74314c9000-7f743161d000 r-xp 00000000 fd:01 27                         /lib64/libc-2.11.1.so
  7f743161d000-7f743181d000 ---p 00154000 fd:01 27                         /lib64/libc-2.11.1.so
  7f743181d000-7f7431821000 r--p 00154000 fd:01 27                         /lib64/libc-2.11.1.so
  7f7431821000-7f7431822000 rw-p 00158000 fd:01 27                         /lib64/libc-2.11.1.so
  7f7431822000-7f7431827000 rw-p 00000000 00:00 0 
  7f7431827000-7f7431846000 r-xp 00000000 fd:01 20                         /lib64/ld-2.11.1.so
  7f7431a14000-7f7431a17000 rw-p 00000000 00:00 0 
  7f7431a44000-7f7431a45000 rw-p 00000000 00:00 0 
  7f7431a45000-7f7431a46000 r--p 0001e000 fd:01 20                         /lib64/ld-2.11.1.so
  7f7431a46000-7f7431a47000 rw-p 0001f000 fd:01 20                         /lib64/ld-2.11.1.so
  7f7431a47000-7f7431a48000 rw-p 00000000 00:00 0 
  7fff62f8c000-7fff62fa2000 rw-p 00000000 00:00 0                          [stack]
  7fff62fff000-7fff63000000 r-xp 00000000 00:00 0                          [vdso]
  ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
  </code></pre>
  
  <p>I can't figure what i am doing wrong, because i was able to run my code if i changed my realloc line as below:</p>
  
  <pre><code>path = (char **) realloc(sizeof(char *)*8);
  </code></pre>
  
  <p>If is did that multiplication by 8 my code runs but i want to allocate exact memory i want, Any ideas why it is not working without that multiply by 8.</p>