Nad*_*LEM 7 c++ stack-overflow debugging g++ visual-c++
你能举一个C++中的堆栈溢出的例子吗?除了递归情况:
void foo() { foo(); }
Run Code Online (Sandbox Code Playgroud)
Tal*_*eff 17
不涉及无限递归的典型情况是在堆栈上声明一个太大的自动变量.例如:
int foo()
{
int array[1000000];
}
Run Code Online (Sandbox Code Playgroud)
void function()
{
function();
}
Run Code Online (Sandbox Code Playgroud)
这是实践中可能发生的事情:
int factorial(int x) {
return x == 0 ? 1 : x * factorial(x-1);
}
Run Code Online (Sandbox Code Playgroud)
这会使堆栈溢出为负数x.并且,正如Frank Krueger所提到的,也是因为太大x(但之后int会先溢出).