我以递归方式调用一个函数 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
非常深的递归和大的堆栈变量以及递归是一些容易导致堆栈溢出的原因。
您可能想编写更智能的代码来摆脱递归。
以下链接可能会帮助您实现目标。
关于c - 147次递归调用后,发生段错误。 (在C中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24137278/