堆栈溢出错误或异常? [英] Stack overflow error or exception?

查看:139
本文介绍了堆栈溢出错误或异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下列结果没有错误?

Why does the following ends up with no error?

void func()
{
   func();
}

int main()
{
   func();
}


推荐答案

/ em>,它会溢出堆栈(因为,即使没有使用局部变量,每次调用都会在堆栈中添加前一个返回地址); 在实践中,启用优化后,由于尾调用优化,它不会溢出实际上避免了在跳转中转换调用的任何资源消耗,因此不会消耗堆栈。

In theory, it would overflow the stack (because, even if no local variables are used, each call would add the previous return address on the stack); in practice, with optimizations enabled, it doesn't overflow because of tail call optimization, which actually avoids any resource consumption transforming the call in a jump, thus not consuming the stack.

请注意,这与as if规则的C ++标准一致:编译的程序必须运行,就像它是你在代码中请求的(效果),并且由于堆栈大小只是一个实现限制,生成的代码使用调用,而使用 jmp 的方法是等同的。

Notice that this is coherent with the C++ standard for the "as if" rule: the compiled program must run as if it were what you requested in the code (in terms of effect), and since the stack size is just an implementation limit, the generated code that uses a call and the one that uses a jmp are equivalent.

这篇关于堆栈溢出错误或异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆