1

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.

Kelsie
  • 1,000
  • 1
  • 9
  • 21
  • None of these functions are declared as inline in the headers. Thanks for the suggestion though. Also I like your name :D – Kelsie Jul 11 '13 at 02:21
  • The compiler is free to inline any function, even if not declared inline. Any compiler worth its salt will inline functions as a matter of course. – zindorsky Jul 11 '13 at 02:35
  • 1
    @zindorsky: would that sort of behavior be limited to release/shipping builds? – Kelsie Jul 11 '13 at 02:52
  • Nevermind, I found an answer to that question: http://stackoverflow.com/questions/1626248/does-gcc-inline-c-functions-without-the-inline-keyword – Kelsie Jul 11 '13 at 02:59

2 Answers2

4

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.

wrdieter
  • 2,836
  • 1
  • 21
  • 19
0

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

Community
  • 1
  • 1
Kelsie
  • 1,000
  • 1
  • 9
  • 21