title: Invalid free() / delete / delete[] / realloc()
body: <p>This is my code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt; 
  
  int main() {
  
      setvbuf(stdout, NULL, _IONBF, 0);
      setvbuf(stderr, NULL, _IONBF, 0);
  
      int i=0;
      int j=0;
      int x=1;
      int *numOfPhases=&amp;x;
      char** nameOfPhases=malloc((*numOfPhases)*sizeof(*nameOfPhases));
      char* stTemp;
      //TODO: check if NULL
      for (i=0; i&lt;(*numOfPhases);i++) {
          nameOfPhases[i]=malloc(sizeof(char));
          char *st=nameOfPhases[i];
          printf("enter char\n");
          do {
              stTemp=malloc(st,sizeof(char)*(j+1));
              //TODO: check if NULL
              st[j]=getchar();
              j++;
          } while (st[j]!='\n');
          if (j&gt;=1) {
              st[j-1]='\0';
          }
          st[j]=0;
          printf("%s \n", nameOfPhases[i]);
          j=0;
      }
  
      for (i=0;i&lt;(*numOfPhases);i++) {
          printf("%s ", nameOfPhases[i]);
      }
  
      for (i=0;i&lt;(*numOfPhases);i++) {
          free(nameOfPhases[i]);
      }
  
      free(nameOfPhases);
      return 0;
  }
  </code></pre>
  
  <p>When I run this on Windows, everything works just fine. However, when I run this on Unix via <a href="http://valgrind.org/" rel="nofollow"><code>valgrind</code></a> I encounter a few errors such as:</p>
  
  <pre><code>==10215== Invalid read of size 1
  ==10215==    at 0x343F047E2C: vfprintf (in /lib64/libc-2.12.so)
  ==10215==    by 0x343F0495DF: buffered_vfprintf (in /lib64/libc-2.12.so)
  ==10215==    by 0x343F04421D: vfprintf (in /lib64/libc-2.12.so)
  ==10215==    by 0x343F04F189: printf (in /lib64/libc-2.12.so)
  ==10215==    by 0x400837: main 
  ==10215==  Address 0x4c23090 is 0 bytes inside a block of size 1 free'd
  ==10215==    at 0x4A06C20: realloc (vg_replace_malloc.c:662)
  ==10215==    by 0x4007CC: main 
  ==10215== 
  ==10215== Invalid free() / delete / delete[] / realloc()
  ==10215==    at 0x4A06430: free (vg_replace_malloc.c:446)
  ==10215==    by 0x4008AF: main 
  ==10215==  Address 0x4c23090 is 0 bytes inside a block of size 1 free'd
  ==10215==    at 0x4A06C20: realloc (vg_replace_malloc.c:662)
  ==10215==    by 0x4007CC: main 
  ==10215== 
  ==10215== 
  ==10215== HEAP SUMMARY:
  ==10215==     in use at exit: 1 bytes in 1 blocks
  ==10215==   total heap usage: 5 allocs, 5 frees, 12 bytes allocated
  ==10215== 
  ==10215== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
  ==10215==    at 0x4A06C20: realloc (vg_replace_malloc.c:662)
  ==10215==    by 0x4007CC: main (in /homet2/ayeletk/OG/main.exe)
  ==10215== 
  ==10215== LEAK SUMMARY:
  ==10215==    definitely lost: 1 bytes in 1 blocks
  ==10215==    indirectly lost: 0 bytes in 0 blocks
  ==10215==      possibly lost: 0 bytes in 0 blocks
  ==10215==    still reachable: 0 bytes in 0 blocks
  ==10215==         suppressed: 0 bytes in 0 blocks
  ==10215== 
  ==10215== For counts of detected and suppressed errors, rerun with: -v
  ==10215== ERROR SUMMARY: 4 errors from 3 contexts (suppressed: 6 from 6)
  </code></pre>
  
  <p>Does anyone have any idea why this could happen?</p>