1

I thought that calling a inline function inside of itself wouldn't be allowed because it would lead to something like infinite source code when the function's invokations get replaced with its body(if that happends). But, when I test is that allowed, I get a Segmentation fault. Example code:

static inline void a(void){
    a();
}
int main(void){
    a();
    return 0;
}

I want to ask why is not a compile-error generated in the first place, and also why this leads to a Segmentation fault. Thanks for reaching out.

markoj
  • 150
  • 10

1 Answers1

2

As @HolyBlackCat mentioned - inline is only a hint for a compiler. Compiler ignores it in a lot of cases.

As for your test - your recursive code causes a crash because of stack overflow error - every function call reserves a memory block on the stack and your program runs out of memory.

leo
  • 387
  • 1
  • 6
  • I was surprised that it would compile, so I didn't check wheter the same thing happends when the function isn't inline. Also, would a compilation error occur if the compiler wouldn't have ignored the `inline` keyword? – markoj Aug 01 '22 at 09:08
  • 1
    Speaking for msvc & gcc they would just ignore the directive. I've never seen compilation errors because of improper `inline` – leo Aug 01 '22 at 09:11
  • Somebody said that it would lead to infinite compile time. – markoj Aug 01 '22 at 09:16
  • 1
    @markoj Then ask this _somebody_ for a working example. Unless that is provided, I doubt this claim. – the busybee Aug 01 '22 at 09:21