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

C字符串函数可以工作,但在多次调用malloc后失败,c,string,memory-management,malloc,C,String,Memory Management,Malloc,最终编辑:这是一个与字符串函数和malloc都不相关的堆栈溢出问题。GDB的输出说问题出在它所在位置的上面几行,这让我很困惑,但我花时间在Valgrind中运行后,我就找到了答案 我编写了一个双向广度优先搜索程序,用于在一个非常大的有向图(约600万个节点)中查找最短路径。有了100个节点的测试输入文件,一切都很好。在输入满的情况下,会使用更多的内存,程序就会出现分段错误 当我在n=sprintf(result,“”)处清理结果缓冲区时,GDB说它在搜索函数的开始处执行segfaulting。以

最终编辑:这是一个与字符串函数和malloc都不相关的堆栈溢出问题。GDB的输出说问题出在它所在位置的上面几行,这让我很困惑,但我花时间在Valgrind中运行后,我就找到了答案

我编写了一个双向广度优先搜索程序,用于在一个非常大的有向图(约600万个节点)中查找最短路径。有了100个节点的测试输入文件,一切都很好。在输入满的情况下,会使用更多的内存,程序就会出现分段错误

当我在
n=sprintf(result,“”)处清理结果缓冲区时,GDB说它在搜索函数的开始处执行segfaulting。以下是相关功能:

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解决了问题,现在一切都按预期运行。谢谢大家的建议

这是一个猜测,但试试这个简单的改变:

在向
结果
变量写入内容的地方,请尝试使用

n = snprintf(result, 2000, "...", ...);
在哪里。。。表示实际要写入
结果
字符串的内容


如果写入超过
结果分配的末尾,效果将不可预测。

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

您是否在
bidirbfs
末尾返回
result
?您确定
sprintf
没有写入超过缓冲区的末尾吗?(改用
snprintf
)我看不到重复调用malloc的地方?这是否在
readStructureFromFile
中?也许我只是个守旧的人,但把分配的大小和缓冲区一起传递似乎更安全。你有没有用valgrind或类似的工具?一般来说,它使解决这类问题变得微不足道(在c中开发时,我默认使用valgrind,这非常有用)。
malloc被调用1300万次……
!我明白了…@thiruvaluvar似乎只有结果缓冲区是2000个字符大。OP说每个顶点的空间是根据入站和出站边缘数组的大小动态分配的。@andrewcooke对不起,我想你已经失去了rep,是吗?我最初是根据你的建议做的,我应该告诉你写一个答案,这样你就可以得到信任。抱歉…我想我没有错。:)我的错,不是你的错。很高兴这有帮助。