What you see is the effect stack overflow that happens due to the infinite recursion. While this is in theory should just print "Hello World" infinitely, the usage of a()
and b()
forces to store the "return address" on the stack. Typical implementations use a fixed size stack and thus it recursive calls eventually results in overflow of call stack.
Note that the "stack" is common terminology and isn't specified in the standard nor is it required to be used.
But if you optimise your code, it could simply be turned into an infinite loop without the use of functions or recursion. For example, your code can be transformed into:
#include<iostream>
int main()
{
while (true)
std::cout<<"Hello World\n";
}
This is allowed and valid under the "as-if" rule of C++ standard. In this case, you are not going to encounter stack overflow or segfault.
Compiling & running your code with:
g++ -Wall -Wextra -O3 test.cpp
doesn't, in fact, result in segfault.
So what you see isn't guaranteed by the standard.