c - 147次递归调用后,发生段错误。 (在C中)

标签 c codeblocks

我以递归方式调用一个函数 147 次,当它调用 147 次时,程序 exe 停止(代码块)。

在再次调用函数之前,它将 1 个 int 全局变量分配给 local,1 个 int 二维全局数组分配给 local,1 个 string 全局变量分配给 local 变量。那么,其中 146 个可能对程序来说是一个非常巨大的负载?

功能是:

最佳答案

看来你的stack is overflowing通过递归调用。 引用自上述维基页面

In software, a stack overflow occurs when the stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash

非常深的递归和大的堆栈变量以及递归是一些容易导致堆栈溢出的原因。

您可能想编写更智能的代码来摆脱递归。

以下链接可能会帮助您实现目标。

  1. Way to go from recursion to iteration
  2. Replace Recursion with Iteration

关于c - 147次递归调用后,发生段错误。 (在C中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24137278/

相关文章:

c - 加速 C 中的 hexadoku(16 x 16,a - p)

C程序查找给定日期的星期几

c - 为什么我的客户端没有收到任何字节?

C : unknown memory issue ? - 二叉树练习

c++ - 将 dll 库导入 CodeBlocks 链接器时遇到问题

c - SDL 事件处理不起作用

c++ - 使用 C++ 中的 OpenMP,矩阵乘法的性能保持不变

c - 使用 Dup2 重定向输入和输出

c++ - while循环中的除法

c++ - 安装了 Ftdilib 但 ftdi_enable_bitbang "was not declared in this scope"