2 ms·
I got curious about this. It seems that C11 at least is much closer to the C++ behaviour: under certain conditions, compilers can assume that loops terminate ev
by wzdd 3y ago
I got curious about this. It seems that C11 at least is much closer to the C++ behaviour: under certain conditions, compilers can assume that loops terminate even if they can't prove it. See https://krinkinmu.github.io/2021/12/12/pain-of-infinite-loops.html https://krinkinmu.github.io/2021/12/12/pain-of-infinite-loop... for example and the relevant part of the standard, N1509 (Clang's compatibility chart referencing that: https://clang.llvm.org/c_status.html https://clang.llvm.org/c_status.html).
Importantly (see for example https://blog.regehr.org/archives/140 https://blog.regehr.org/archives/140) it's not that the standard previously explicitly forbade this sort of optimisation, it's just that it wasn't very clear and compiler writers interpreted it differently.
So in summary, yes, they're different languages, but "modern" C is is less different for this particular thing.
- ryao 3y agoHow they treat infinite loops that obviously never terminate (e.g. while (1)) is probably of greater interest. The first link gives this code: namespace { void Panic() { while (1); } } // namespace extern "C" void kernel() { Panic(); } The clang++ really does optimize this away: https://godbolt.org/z/1Wf135918 https://godbolt.org/z/1Wf135918 The same thing happens when I let clang++ compile a C version as C++: https://godbolt.org/z/MxcGfWhej https://godbolt.org/z/MxcGfWhej However, if I compile it as C with clang, I get an infinite loop: https://godbolt.org/z/x63fo33Wa https://godbolt.org/z/x63fo33Wa