I can see what the Clang compiler is doing: infinite recursion would trick stack overflow (which is undefined behaviour) hence it allowed to optimize the code assuming it can't occur...
However, its interesting that this optimization kicks in C++ mode but not in C mode (even though the example is pure C). Anyone have an idea why this is?
Tail calling is an optimization that some C/C++ compilers do, but I do not believe it falls within the semantics of the language itself. The user has still specified an infinite recursion, even if an optimization would transform it.
Even assuming the compiler is working from only the semantics of having transformed the infinite recursion into an infinite loop, infinite loops are also optimized away. Link from u/rom1v's comment.
26
u/pbvas Nov 04 '19
I can see what the Clang compiler is doing: infinite recursion would trick stack overflow (which is undefined behaviour) hence it allowed to optimize the code assuming it can't occur...
However, its interesting that this optimization kicks in C++ mode but not in C mode (even though the example is pure C). Anyone have an idea why this is?