堆栈运行时错误

use*_*009 0 c++ stack runtime-error

#include <stack>
#include <functional>

int main()
{
    std::stack<std::function<int()>> s;
    s.push(main);
    return s.top()();
}
Run Code Online (Sandbox Code Playgroud)

我得到以下诊断:

result: runtime error        time: 0.04s    memory: 39704 kB     signal: 11 (SIGSEGV)
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?

Mar*_*k B 6

首先,你不能给main自己打电话.其次,它似乎正在做"你所期望的"并进行调用,因此你会导致无限递归,这会占用你所有的堆栈空间然后溢出它.