C 字符串函数可以工作,但在多次调用 malloc 后会失败

标签 c string memory-management malloc

最终编辑:事实证明这是一个堆栈溢出问题,与字符串函数和 malloc 都无关。 GDB 输出显示问题出在上面几行,这让我很困惑,但是当我花时间在 Valgrind 中运行时,我就弄清楚了。

我编写了一个双向广度优先搜索程序,用于在非常大的有向图(约 600 万个节点)内查找最短路径。使用 100 个节点的测试输入文件,一切都运行良好。使用完整的输入时,会使用更多的内存,并且程序会出现段错误。

当我在 n = sprintf(result, ""); 处清理结果缓冲区时,GDB 表示在搜索函数开始时出现段错误。相关函数如下:

char *bidirbfs(int x, int y, char *result){
    int n;
    n = sprintf(result, "");
    ...

这是对其的调用以及结果缓冲区的分配:

int main (){
    int n=0;
    char *result;
    result = (char *)malloc(sizeof(char)*2000);
    if(result == NULL){
        printf("MALLOC FAILED!"); exit(1);}

    //Methods for initializing graph
    readStructureFromFile(); 
    calcArticlesIn();

    //Search the graph
    result = bidirbfs(1,2, result);
    printf("%s\n", result);
    ...
}

同样,只要输入很小,一切就可以了。当我使用全尺寸输入时,程序可以正常读取所有内容,但随后会出现段错误。当我使用对 strncpy 的非常相似的调用来清空数组时,我得到了相同的行为,所以看起来这是一个普遍问题与字符串函数。我不确定会发生什么。

似乎 sprintf 不喜欢它所获得的指针,这让我想知道 malloc 是否在做一些奇怪的事情。当使用完整输入时,malloc 被调用 1300 万次*,所以我想知道它是否可能因此而表现出奇怪的行为,并用一些奇怪的东西覆盖字符串缓冲区。同时,我又很犹豫是否要责怪图书馆。

有什么想法会发生什么吗?

*可悲的是我认为这实际上是必要的。图中的每个元素都有一个用于入站边缘和出站边缘的数组。每个数组的大小在读取输入之前都是未知的,因此必须通过 malloc 将其动态分配到正确的大小。

编辑:Valgrind 返回以下内容。我正在努力弄清楚它可能意味着什么,但乍一看它实际上可能是某种堆栈溢出。

==27263== Warning: client switching stacks?  SP change: 0xbea50634 --> 0xbb815340
==27263==          to suppress, use: --max-stackframe=52671220 or greater
==27263== Invalid write of size 4
==27263==    at 0x8048D78: bidirbfs (load_data.c:184)
==27263==    by 0x80491CD: main (load_data.c:304)
==27263==  Address 0xbb815348 is on thread 1's stack
==27263== 
==27263== 
==27263== Process terminating with default action of signal 11 (SIGSEGV)
==27263==  Access not within mapped region at address 0xBB815348
==27263==    at 0x8048D78: bidirbfs (load_data.c:184)
==27263==  If you believe this happened as a result of a stack
==27263==  overflow in your program's main thread (unlikely but
==27263==  possible), you can try to increase the size of the
==27263==  main thread stack using the --main-stacksize= flag.
==27263==  The main thread stack size used in this run was 8388608.
==27263== 
==27263== Process terminating with default action of signal 11 (SIGSEGV)
==27263==  Access not within mapped region at address 0xBB81533C
==27263==    at 0x401F4DD: _vgnU_freeres (vg_preloaded.c:58)
==27263==  If you believe this happened as a result of a stack
==27263==  overflow in your program's main thread (unlikely but
==27263==  possible), you can try to increase the size of the
==27263==  main thread stack using the --main-stacksize= flag.
==27263==  The main thread stack size used in this run was 8388608.
==27263== 
==27263== HEAP SUMMARY:
==27263==     in use at exit: 1,021,539,288 bytes in 13,167,791 blocks
==27263==   total heap usage: 13,167,792 allocs, 1 frees, 1,047,874,864 bytes allocated
==27263== 
==27263== LEAK SUMMARY:
==27263==    definitely lost: 0 bytes in 0 blocks
==27263==    indirectly lost: 0 bytes in 0 blocks
==27263==      possibly lost: 0 bytes in 0 blocks
==27263==    still reachable: 1,021,539,288 bytes in 13,167,791 blocks
==27263==         suppressed: 0 bytes in 0 blocks
==27263== Rerun with --leak-check=full to see details of leaked memory
==27263== 
==27263== For counts of detected and suppressed errors, rerun with: -v
==27263== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 7)

编辑2: 最终解决方案:这是堆栈溢出。在 sprintf 语句之后,我创建了一个数组,其大小与节点数成正比。由于我没有使用 malloc,因此它是直接在堆栈上创建的,导致堆栈溢出。更改为使用 malloc 解决了问题,现在一切都按预期运行。感谢大家的建议!

最佳答案

在 valgrind 中运行您的程序。看看它怎么说。我打赌您会发现输出很有启发。

关于C 字符串函数可以工作,但在多次调用 malloc 后会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10158024/

相关文章:

c - 为什么是 SIGSEGV?

C: SEGFAULT with realloc on char **

c - argv 中指向字符串的指针是否可修改?

java - 将 '==' 与字符串一起使用? ( java )

python - 我想用列表中的双引号替换单引号

javascript - 我如何在 JavaScript 中按每第 n 个字符拆分字符串?

Java 小程序在关闭时不会完全进行垃圾收集

ios - iOS8 中的内存管理问题,Objective C

c - 使用 scanf 和 do while 循环读取文件

C 管理函数临时动态内存的有效方法