c - Why realloc of char** gives Address is 0 bytes after alloc'd -


i have following code:

#include <stdio.h> #include <string.h> #include <stdlib.h>  int main(int argc, char *argv[]) {   char* filename = "file_prefix.txt";   file* file_prefix = fopen(filename, "r");    char buff[1024];   int = 0;   char** prefix = null;    char c = fscanf(file_prefix, "%s", buff);   while ( eof != c )   {     printf("%d : %s\n", i, buff);     char** temp  = realloc(prefix, sizeof(char*) * (i+1));     temp[i] = malloc( (sizeof(char) * strlen(buff)) + 1);     strcpy(temp[i], buff );     prefix = temp;     memset(buff, 0, sizeof(buff));     c = fscanf(file_prefix, "%s", buff);     ++i;   }     int x = 0;   (;x < i; ++x) {     printf("%s\n", prefix[i]);   }   free(prefix);    fclose(file_prefix);   return 0; } 

assuming file_prefix.txt exists following valgrind:

==7322== memcheck, memory error detector ==7322== copyright (c) 2002-2013, , gnu gpl'd, julian seward et al. ==7322== using valgrind-3.10.0.svn , libvex; rerun -h copyright info ==7322== command: ./main ==7322==  0 : pdf 1 : txt ==7322== invalid read of size 8 ==7322==    @ 0x400a6d: main (main.c:29) ==7322==  address 0x51fc370 0 bytes after block of size 16 alloc'd ==7322==    @ 0x4c2ce8e: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==7322==    0x400976: main (main.c:18) ==7322==  ==7322== invalid read of size 1 ==7322==    @ 0x4c2e0e2: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==7322==    0x4ea6e3b: puts (ioputs.c:36) ==7322==    0x400a77: main (main.c:29) ==7322==  address 0x0 not stack'd, malloc'd or (recently) free'd ==7322==  ==7322==  ==7322== process terminating default action of signal 11 (sigsegv) ==7322==  access not within mapped region @ address 0x0 ==7322==    @ 0x4c2e0e2: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==7322==    0x4ea6e3b: puts (ioputs.c:36) ==7322==    0x400a77: main (main.c:29) ==7322==  if believe happened result of stack ==7322==  overflow in program's main thread (unlikely ==7322==  possible), can try increase size of ==7322==  main thread stack using --main-stacksize= flag. ==7322==  main thread stack size used in run 8388608. ==7322==  ==7322== heap summary: ==7322==     in use @ exit: 592 bytes in 4 blocks ==7322==   total heap usage: 5 allocs, 1 frees, 600 bytes allocated ==7322==  ==7322== leak summary: ==7322==    lost: 0 bytes in 0 blocks ==7322==    indirectly lost: 0 bytes in 0 blocks ==7322==      possibly lost: 0 bytes in 0 blocks ==7322==    still reachable: 592 bytes in 4 blocks ==7322==         suppressed: 0 bytes in 0 blocks ==7322== reachable blocks (those pointer found) not shown. ==7322== see them, rerun with: --leak-check=full --show-leak-kinds=all ==7322==  ==7322== counts of detected , suppressed errors, rerun with: -v ==7322== error summary: 2 errors 2 contexts (suppressed: 0 0) [1]    7322 segmentation fault (core dumped)  valgrind --leak-check=full ./main 

what problem with:

char** temp  = realloc(prefix, sizeof(char*) * (i+1)); 

??

fscanf not return eof, returns the number of input items assigned (man fscanf). purposes, need check

 while ( c == 1 )    ... 

however, in not cause segfault! caused attempting print prefix[i] (repeatedly), not set correct value. meant print prefix[x] instead:

for (;x < i; ++x) {   printf("%s\n", prefix[x]); } 

with these 2 corrections, program runs without error.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -