【问题标题】:Valgrind reporting possible stack overflow - what does it mean?Valgrind 报告可能的堆栈溢出 - 这是什么意思?
【发布时间】:2011-12-18 13:02:10
【问题描述】:

当我使用 valgrind 运行我的 C 程序文件时,我收到以下错误和分段错误。什么意思?

由于递归调用更深的一个函数而出现此错误

我为字符指针分配了 9684173 个字节 我 malloc 成功,我在一个函数的递归调用中遇到了分段错误。那么有什么解决办法吗??

==3100== Stack overflow in thread 1: can't grow stack to 0x7fe801ff8
==3100== 
==3100== Process terminating with default action of signal 11 (SIGSEGV)
==3100==  Access not within mapped region at address 0x7FE801FF8
==3100==    at 0x4014D8: huffman_decoding (filebits.c:471)
==3100==  If you believe this happened as a result of a stack
==3100==  overflow in your program's main thread (unlikely but
==3100==  possible), you can try to increase the size of the
==3100==  main thread stack using the --main-stacksize= flag.
==3100==  The main thread stack size used in this run was 8388608.
==3100== Stack overflow in thread 1: can't grow stack to 0x7fe801ff0
==3100== 
==3100== Process terminating with default action of signal 11 (SIGSEGV)
==3100==  Access not within mapped region at address 0x7FE801FF0
==3100==    at 0x4A2269F: _vgnU_freeres (vg_preloaded.c:58)
==3100==  If you believe this happened as a result of a stack
==3100==  overflow in your program's main thread (unlikely but
==3100==  possible), you can try to increase the size of the
==3100==  main thread stack using the --main-stacksize= flag.
==3100==  The main thread stack size used in this run was 8388608.
==3100== 
==3100== HEAP SUMMARY:
==3100==     in use at exit: 22,490,801 bytes in 179 blocks
==3100==   total heap usage: 179 allocs, 0 frees, 22,490,801 bytes allocated
==3100== 
==3100== LEAK SUMMARY:
==3100==    definitely lost: 0 bytes in 0 blocks
==3100==    indirectly lost: 0 bytes in 0 blocks
==3100==      possibly lost: 0 bytes in 0 blocks
==3100==    still reachable: 22,490,801 bytes in 179 blocks
==3100==         suppressed: 0 bytes in 0 blocks
==3100== Reachable blocks (those to which a pointer was found) are not shown.
==3100== To see them, rerun with: --leak-check=full --show-reachable=yes
==3100== 
==3100== For counts of detected and suppressed errors, rerun with: -v
==3100== Use --track-origins=yes to see where uninitialised values come from
==3100== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 4 from 4)
Segmentation fault

【问题讨论】:

  • 我们需要查看一些代码才能提供帮助。
  • @IgnacioVazquez-Abrams 立即阅读
  • 我得到了这个问题的解决方案

标签: c memory out-of-memory bit


【解决方案1】:

您要么试图在堆栈上分配太多,要么调用的递归函数太深。

由于您提到“我想压缩一个大小为 12 MB 的大文件”,我强烈感觉您正在尝试在堆栈上分配 12M。

【讨论】:

  • 试图调用递归函数太深
  • 我得到这个错误是由于一个函数的递归调用更深我为我成功完成的字符指针分配了 9684173 个字节,并且我在递归调用一个函数时遇到了分段错误。那么有什么解决方案吗??
【解决方案2】:

此消息表示您正在尝试在堆栈中分配大量数据,要解决此问题,请尝试尽可能释放堆栈,或者您必须使用 ulimit 命令增加堆栈大小

ulimit -s new_size

或使用 C 中的函数“setrlimit”。

当然我不推荐这样,如果你要使用大堆栈大小,请改用 malloc。

【讨论】:

  • 我如何知道我当前的堆栈最大大小?
  • 使用“ulimit -s”或使用getrlimit函数
  • 哎呀!你为什么要做这么大的堆栈?将文件内容放在分配的内存中而不是在堆栈上,这只是可怕的编程实践。通常,Linux 系统将堆栈的软限制设置为 8192 KB,这对于标称程序中的所有调用深度应该足够了。
  • @ahm.madkour 我们可以直接设置栈大小吗?
  • @ahm.madkour 我们如何将文件内容从堆栈中移动到分配的内存中
猜你喜欢
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 2013-08-08
  • 2011-10-09
  • 1970-01-01
相关资源
最近更新 更多