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
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top