奇怪的段错误,可能与realloc有关 [英] strange seg fault, likely with realloc
问题描述
char *dumpTB (TB tb){
char* text = malloc(sizeof(char));
int i = 0; //
int x = 0; //string index
tNode* curr = tb->head;
while(curr != NULL){
while(curr->line[x] != '\n'){
printf("%d", i);
text[i] = curr->line[x];
printf("%c\n", text[i]);
text = realloc(text, i+1);
i++;
x++;
}
text[i] = '\n';
printf("%c", text[i]);
text = realloc(text, i+1);
i++;
x = 0;
curr = curr->next;
}
return text;
}
所以我设法使用打印输出字符串的前12个字母语句,但是由于某种原因,它在打印第12个字母 l后不久给我一个段错误,并且基于打印语句,它似乎发生在重新分配周围...有人可以告诉我我做错了什么吗?
So I manage to print out the first 12 letters of my string using the print statements but for some reason it gives me a seg fault shortly after printing the 12th letter 'l', and based on the print statements it seems to occur around the realloc...can anyone tell me what I did wrong?
int i = 1; //
int x = 0; //string index
tNode* curr = tb->head;
while(curr != NULL){
while(curr->line[x] != '\n'){
printf("%d", i-1);
text[i-1] = curr->line[x];
printf("%c\n", text[i-1]);
text = realloc(text, i+1);
i++;
x++;
}
printf("%d\n", i-1);
text[i-1] = '\n';
printf("%c", text[i-1]);
text = realloc(text, i+1);
i++;
x = 0;
curr = curr->next;
//printf("%c\n", curr->line[0]);
}
我试图修复索引错误,这是一个看起来很长的sysmalloc断言东西,会中止
I tried fixed the index errors, a really long looking sysmalloc assertion thing which aborts the program.
推荐答案
您可能想要 getline(3),因此使用如果有的话。如 AnT的答案所述,您有未定义的行为(UB),原因是缓冲区溢出一个>。尽快解决。对UB非常害怕,并花更多的时间来了解它(这很棘手)。
You probably want getline(3), so use it if you have it. As explained by AnT's answer, you have undefined behavior (UB) because of a buffer overflow. Fix that as soon as possible. Be very scared of UB and take more time to read about it (it is tricky).
另外,请记住,两个 malloc(3 )和 realloc(3)昂贵的通话(对于昂贵的一些适当概念;实际上,它们非常快-合理使用时通常在桌面上不到一微秒),并且它们可能会失败(通过提供 NULL
),您应该检查。
Also, remember that both malloc(3) and realloc(3) are somehow expensive calls (for some suitable notion of expensive; actually, they are quite fast - often less than a microsecond on a desktop for reasonable use), and they could fail (by giving NULL
) and you should check that.
实际上,您最好减少使用 realloc
的频率。根据经验,您希望将使用的长度和分配的大小都保留在某个位置(例如,在其他局部变量中),并且可能使用一些几何级数(例如, newsize = 4 * oldsize / 3 + 10
....),以避免太频繁地 realloc
-s。为每个额外的字节执行 realloc
是很难的。
In practice, you'll better use realloc
less often. As a rule of thumb, you want to keep both the used length and the allocated size somewhere (e.g. in additional local variables), and you might use some geometrical progression (e.g. newsize = 4*oldsize/3 + 10
....) to avoid too frequent realloc
-s. Doing a realloc
for each additional byte is ugly.
使用所有警告和调试信息来编译代码,例如 gcc -Wall -Wextra -g
和 GCC 。改进代码以获得任何警告。 使用调试器 gdb
和< a href = http://valgrind.org/ rel = nofollow noreferrer> valgrind 来狩猎内存泄漏和其他麻烦。
Compile your code with all warnings and debug info, e.g. gcc -Wall -Wextra -g
with GCC. Improve the code to get no warnings. Use the debugger gdb
and valgrind to hunt memory leaks and other trouble.
这篇关于奇怪的段错误,可能与realloc有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!