我正在尝试读取一个字符串
char *string=malloc(sizeof(char));
char *start_string=string; //pointer to string start
while ((readch=read(file, buffer, 4000))!=0){ // read
filelen=filelen+readch; //string length
for (d=0;d<readch;d++)
*start_string++=buffer[d]; //append buffer to str
realloc(string, filelen); //realloc with new length
有时这会崩溃并出现以下错误:
malloc: *** error for object 0x1001000e0: pointer being realloc'd was not allocated
但有时不是,我不知道如何解决它。