gpt4 book ai didi

c - realloc:下一个大小错误无效

转载 作者:行者123 更新时间:2023-12-02 09:34:29 25 4
gpt4 key购买 nike

我在 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 时,我都会将 num_array_sz 的大小加倍256.

在我自己的计算机上,我用比学校计算机更多的输入对此进行了测试,并且它运行完美。

我很好奇这是否是由于我学校计算机上的内存有限或可能是平台差异造成的。

这是我在学校计算机上运行的 valgrind 输出:

==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)

这些错误指向 realloc 行。

最佳答案

您没有正确使用realloc

linenum_tmp = realloc(tail->line_numbers, ....
...
*(tail->line_numbers + counter - 1) = ...

如果realloc需要重新分配内存,则传递给它的指针将释放。然后,您继续使用 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 行),您正在访问之前空闲的内存d.供您引用,它之前是由 realloc 释放的,它是由 rgpp_v2.c 第 177 行的 process_input 调用的。

关于c - realloc:下一个大小错误无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com