c - double free or corruption (fasttop) -
this code reads null terminated string file (file containing null terminated strings & should provide index wanted string).
error:
*** error in `./main': double free or corruption (fasttop): 0x090a4a80 *** aborted
code:
char *tmp_realloc=null; char *sstring=null; int i=1; /*set file pointer point string*/ fseek(fh,index,seek_set); sstring=(char*)malloc(sizeof(char)); if(sstring == null) return null; tmp_realloc=sstring; while( (*(sstring+i-1)=(char)fgetc((file*)fh)) != 0x0 ) { /*reallocate more memory*/ tmp_realloc=(char*)realloc((char*)sstring,++i); /*if not same address copy old new & set old point new*/ if(tmp_realloc != sstring){ strcpy((char*)tmp_realloc,(char*)sstring); free(sstring); sstring=tmp_realloc; } }
could tell me pointer causing issue (& reason) ?
remove part
/*if not same address copy old new & set old point new*/ if(tmp_realloc != sstring){ strcpy((char*)tmp_realloc,(char*)sstring); free(sstring); sstring=tmp_realloc; }
this (something memcpy()
used instead of strcpy()
) realloc()
if succeeds. instead of that, insert error checking , assign tmp_realloc
sstring
if no error found.
also casting char*
char*
seems useless, why not remove these casts?
Comments
Post a Comment