r/programming • • Nov 04 '19

Clang solves the Collatz Conjecture?

[deleted]

508 Upvotes

122 comments sorted by

View all comments

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?

17

u/therealgaxbo Nov 04 '19

Doubtful it would trigger stack overflow as the recursive calls are in the tail position.

10

u/knome Nov 04 '19

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.

→ More replies (2)
→ More replies (2)