free():C 中的下一个大小(快速)无效
最近几天我一直试图在我的程序中找出这个错误。如果我增加 #define LINE_SIZE
的大小,它在我的测试用例上工作得很好,但这只是对我的问题的临时解决。
在这部分代码中,我读取文件中的每一行,并为我找到的每个特定字符在内存中增加一个位置。 ascii 指向一个 malloc
的 256 个整数区域。
void readFile(FILE *fp, int *ascii) {
char *line;
char *temp;
while (!feof(fp)) {
line = readLine(fp);
if (line == NULL)
break;
temp = line;
while (*temp != '\0') {
(*(ascii + *temp))++;
temp++;
}
free(line); <-------------- BREAKS HERE
}
fclose(fp);
}
char* readLine(FILE *fp) {
char *rtn = NULL;
int last = -1;
int size = LINE_SIZE; <------ LINE_SIZE = 8
do {
rtn = (char*)realloc(rtn,size);
if (!rtn) {
printf("Realloc failed\n");
exit(1);
}
fgets(rtn + last + 1, size, fp);
if (feof(fp))
break;
last = strlen(rtn) - 1;
size += size;
if (rtn[last] == '\n')
break;
} while (rtn[last] != '\0');
return rtn;
}
我试图让我的代码读取它自己的 .c 文件,它在 250 行文件中的第 58 行处中断: void makeList(int *ascii, LinkNode *list) {
在 gdb 中,打印line
就在我释放之前,它给了我:
(gdb) print line
$1 = 0x9849578 "void makeList(int *ascii, LinkNode *list) {\n"
这正是我期望它打印的内容。当代码尝试释放我不再需要的这一行时,代码立即崩溃。
Been trying to figure out this bug for the last couple of days in my program. It works fine on my test cases if I increase the size of my #defined LINE_SIZE
, but that would only be a temporary fix to my problem.
In this part of the code, I read every line in a file, and increment a spot in memory for each specific character I find. ascii points to a malloc
'd area of 256 ints.
void readFile(FILE *fp, int *ascii) {
char *line;
char *temp;
while (!feof(fp)) {
line = readLine(fp);
if (line == NULL)
break;
temp = line;
while (*temp != '\0') {
(*(ascii + *temp))++;
temp++;
}
free(line); <-------------- BREAKS HERE
}
fclose(fp);
}
char* readLine(FILE *fp) {
char *rtn = NULL;
int last = -1;
int size = LINE_SIZE; <------ LINE_SIZE = 8
do {
rtn = (char*)realloc(rtn,size);
if (!rtn) {
printf("Realloc failed\n");
exit(1);
}
fgets(rtn + last + 1, size, fp);
if (feof(fp))
break;
last = strlen(rtn) - 1;
size += size;
if (rtn[last] == '\n')
break;
} while (rtn[last] != '\0');
return rtn;
}
I'm trying to have my code read its own .c file, and it breaks on line 58 in a 250 line file: void makeList(int *ascii, LinkNode *list) {
In gdb, printing line
right before I free it gives me:
(gdb) print line
$1 = 0x9849578 "void makeList(int *ascii, LinkNode *list) {\n"
Which is exactly what I would expect it to print. The code crashes right after, when it tries to free this line that I no longer need.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有将正确的缓冲区大小传递给
fgets()
。即使您分配了那么多字节,您还是将偏移量传递到缓冲区中,并且需要考虑到这一点:You are not passing the correct buffer size to
fgets()
. Even though you're allocating that many bytes, you are passing in an offset into your buffer and you need to account for that:ascii 指向 256*4=1024 字节的区域,并且您将读取的行复制到其中;因此,除非您的行(平均)大约有 20 个字符长,否则您将在到达第 58 行之前运行完该缓冲区的末尾。可能不会导致 free 失败,但它不会有帮助。
ascii points to an area of 256*4=1024 bytes, and you're copying the lines you read into it; so unless your lines are (on average) about 20 characters long, you're going to run off the end of that buffer before you get to line 58. May not be causing free to fail, but it can't be helping.