给定这样的设置,调用 DoFooStuff() 的地方:
class Foo {
public:
void DoFooStuff(); // calls Bar::DoBarStuff()
}
class Bar {
public:
void DoBarStuff(); // Calls Bar::DoInternalBarStuff()
protected:
void DoInternalBarStuff();
}
是什么让我的堆栈跟踪可以准确地显示这个?:
Type Function
void Bar::DoInternalBarStuff()
void Foo::DoFooStuff()
对 DoInternalBarStuff() 的唯一引用是在 DoBarStuff() 中。 DoInternalBarStuff() 在它的第一行断言:
assert(false);
这是获取堆栈跟踪的位置。
最佳答案
调用 Bar::DoBarInternalStuff 是 Bar::DoBarStuff 中的最后一条语句吗?如果是这样,编译器很可能在调用 Bar::DoBarInternalStuff 时用 Bar::DoBarInternalStuff 替换了 Bar::DoBarStuff 的栈帧。
这种tail call优化在 C/C++ 编译器中相当普遍。当可以安排递归函数使得递归调用是函数中的最后一次调用时,它减少了所需的堆栈深度。
关于c++ - 为什么堆栈跟踪会跳过一个显然必须调用才能到达以下函数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583985/