Why would a stack trace skip a function that obviously has to have been called for the following functions to have been reached?

StackOverflow https://stackoverflow.com/questions/17583985

  •  02-06-2022
  •  | 
  •  

문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

So it turns out that the compiler does automatic inlining at certain optimization levels. Learn new things every day. :)

Does GCC inline C++ functions without the 'inline' keyword?

(Thanks for the helpful comments that showed me this.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top