为什么堆栈跟踪会跳过一个显然必须被调用的函数才能达到以下函数?(Why would a stack trace skip a function that obviously has to have been called for the following functions to have been reached?)

给定这样的设置,调用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);

这就是堆栈跟踪的位置。

Given a setup like this, where DoFooStuff() is called:

class Foo { public: void DoFooStuff(); // calls Bar::DoBarStuff() } class Bar { public: void DoBarStuff(); // Calls Bar::DoInternalBarStuff() protected: void DoInternalBarStuff(); }

What could make it possible that my stack trace could show exactly this?:

Type Function void Bar::DoInternalBarStuff() void Foo::DoFooStuff()

The only reference to DoInternalBarStuff() is in DoBarStuff(). DoInternalBarStuff() asserts on it's first line:

assert(false);

And that is the location where the stack trace is taken from.

最满意答案

调用Bar :: DoBarInternalStuff是Bar :: DoBarStuff中的最后一个语句吗? 如果是这样,当调用Bar :: DoBarInternalStuff时,编译器很可能用Bar :: DoBarInternalStuff替换Bar :: DoBarStuff的堆栈帧。

这种尾调用优化在C / C ++编译器中相当普遍。 它减少了可以安排递归函数所需的堆栈深度,使得递归调用是函数中的最后一次调用。

Is the call to Bar::DoBarInternalStuff the last statement in Bar::DoBarStuff? If so, the compiler most likely replaced Bar::DoBarStuff's stack frame with Bar::DoBarInternalStuff's when Bar::DoBarInternalStuff was called.

This kind of tail call optimization is fairly common in C/C++ compilers. It reduces the stack depth required when a recursive function can be arranged such that the recursive call is the last call in the function.

更多推荐