Dieser Code liest eine nullterminierte Zeichenfolge aus einer Datei (Datei mit null terminierten Strings & sollte Index für gewünschte Zeichenfolge bereitstellen).
doppelt frei oder Korruption (FastTop)
Fehler:
*** Error in `./main': double free or corruption (fasttop): 0x090a4a80 ***
Aborted
Code:
char *tmp_realloc=NULL;
char *sstring=NULL;
int i=1;
/*Set file pointer to 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 to new & set old to point new*/
if(tmp_realloc != sstring){
strcpy((char*)tmp_realloc,(char*)sstring);
free(sstring);
sstring=tmp_realloc;
}
}
Können Sie mir sagen, welche Zeiger dieses Problem verursacht (& aus welchem Grund)?
Vielen Dank, es funktioniert jetzt. Also verstehe ich, dass der Fehler von der Tatsache herrührt, dass ich 'free (sstring)' gemacht habe, was bereits von 'realloc' getan wurde, so dass die Speicherbefreiung zweimal auf demselben Zeiger erfolgt ist? –