我在realloc
发生此错误,这只发生在我学校的实验室计算机上而不是我的计算机上。
在这个程序中,我将行号存储在File_Node
结构中。 File_Node
是链接列表的一部分,每个节点包含文件中的文件路径字符串和数组行号。
此程序正常工作,直到需要存储的行号太多(> 3000)。
以下是我的代码的相关部分:
if ((token = strtok(NULL, delim)) != NULL) {
char *endptr = NULL;
int *linenum_tmp = NULL;
long line_number;
errno = 0;
line_number = strtol(token, &endptr, 10);
if (errno == ERANGE) {
exit_program("Integer overflow.");
}
if (*endptr != '\0' || endptr == token || line_number < 0) {
exit_program("Cannot parse line number input.");
}
if (tail->line_numbers == NULL) {
tail->line_numbers = malloc(num_array_sz * sizeof(int));
}
if (counter == num_array_sz) { //Area of interest
num_array_sz *= 2;
if ((linenum_tmp = realloc(tail->line_numbers, sizeof(int) * num_array_sz)) == NULL) {
exit_program("Error in realloc.");
}
}
*(tail->line_numbers + counter - 1) = line_number;
} else {
exit_program("Cannot parse line number input.");
}
counter++;
上面的代码是一个更大的while循环的一部分,它包含更多的行,但我会在必要时发布它。这就是为什么底部有counter++
的原因。每当num_array_sz
表示存储的行数达到counter
时,我的num_array_sz
的大小基本上翻了一倍,256
初始化为==1579== Memcheck, a memory error detector
==1579== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==1579== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==1579== Command: ./rgpp_v2 -w the -b -n
==1579==
==1579== Invalid write of size 4
==1579== at 0x40110E: process_input (rgpp_v2.c:181)
==1579== by 0x400DCC: main (rgpp_v2.c:103)
==1579== Address 0x51e0b9c is 1,020 bytes inside a block of size 1,024 free'd
==1579== at 0x4C29B7E: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==1579== by 0x4010DD: process_input (rgpp_v2.c:177)
==1579== by 0x400DCC: main (rgpp_v2.c:103)
==1579==
==1579== Invalid free() / delete / delete[] / realloc()
==1579== at 0x4C29B7E: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==1579== by 0x4010DD: process_input (rgpp_v2.c:177)
==1579== by 0x400DCC: main (rgpp_v2.c:103)
==1579== Address 0x51e07a0 is 0 bytes inside a block of size 1,024 free'd
==1579== at 0x4C29B7E: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==1579== by 0x4010DD: process_input (rgpp_v2.c:177)
==1579== by 0x400DCC: main (rgpp_v2.c:103)
==1579==
Error in realloc.
==1579==
==1579== HEAP SUMMARY:
==1579== in use at exit: 3,477 bytes in 9 blocks
==1579== total heap usage: 12 allocs, 3 frees, 8,717 bytes allocated
==1579==
==1579== LEAK SUMMARY:
==1579== definitely lost: 2,048 bytes in 1 blocks
==1579== indirectly lost: 0 bytes in 0 blocks
==1579== possibly lost: 0 bytes in 0 blocks
==1579== still reachable: 1,429 bytes in 8 blocks
==1579== suppressed: 0 bytes in 0 blocks
==1579== Rerun with --leak-check=full to see details of leaked memory
==1579==
==1579== For counts of detected and suppressed errors, rerun with: -v
==1579== ERROR SUMMARY: 34 errors from 2 contexts (suppressed: 2 from 2)
。
在我自己的计算机上,我用比学校计算机更多的输入来测试它,它运行得很完美。
我很好奇这是因为我学校的计算机上的ram有限还是平台上的差异。
这是我在学校计算机上运行的valgrind输出:
realloc
这些错误指向{{1}}行。
答案 0 :(得分:2)
您未正确使用realloc
。
linenum_tmp = realloc(tail->line_numbers, ....
...
*(tail->line_numbers + counter - 1) = ...
如果realloc
需要重新分配内存,则传递给它的指针为freed
。然后,您继续使用{em>旧的,已释放的值tail->line_numbers
。
您必须始终使用realloc
的返回值。
我认为你想要的是:
tail->line_numbers = realloc(tail->line_numbers, ...
其次,您误解了valgrind
告诉您的内容。
==1579== Invalid write of size 4
==1579== at 0x40110E: process_input (rgpp_v2.c:181)
==1579== by 0x400DCC: main (rgpp_v2.c:103)
==1579== Address 0x51e0b9c is 1,020 bytes inside a block of size 1,024 free'd
==1579== at 0x4C29B7E: realloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==1579== by 0x4010DD: process_input (rgpp_v2.c:177)
==1579== by 0x400DCC: main (rgpp_v2.c:103)
它的含义是:在功能process_input
(rgpp_v2.c
的第181行)中,您正在访问之前free
d的内存。供您参考,之前已由realloc
释放,process_input
在rgpp_v2.c
的第177行调用。{/ p>
答案 1 :(得分:2)
if (counter == num_array_sz) { //Area of interest
num_array_sz *= 2;
if ((linenum_tmp = realloc(tail->line_numbers, sizeof(int) * num_array_sz)) == NULL) {
exit_program("Error in realloc.");
}
}
*(tail->line_numbers + counter - 1) = line_number;
您需要使用realloc
的结果更新工作变量,如下所示:
if (counter == num_array_sz) { //Area of interest
num_array_sz *= 2;
if ((linenum_tmp = realloc(tail->line_numbers, sizeof(int) * num_array_sz)) == NULL) {
exit_program("Error in realloc.");
}
}
tail->line_numbers = linenum_tmp; // <---!
*(tail->line_numbers + counter - 1) = line_number;