3 ms·
> there are better and worse things compilers can do in the presence of UB, and the big one is warning you about it when it possibly can. GCC should add -Wunde
by gpderetta 4y ago
> there are better and worse things compilers can do in the presence of UB, and the big one is warning you about it when it possibly can.
GCC should add -Wundefined-behaviour flag that will unconditionally print "warning: this program might or not have UB" as the first diagnostic.
- jxf 4y ago60% of the time, it's right every time!
- torstenvl 4y agoIt should also warn if there are infinite loops, so the dev can know whether it will ever halt.
- marcosdumay 4y ago"This program may or may not loop forever" Seems pretty viable.
- Kranar 4y agoInfinite loops are undefined behavior in C++.
- deleted 4y ago[deleted]
- Someone 4y agoNo, they aren’t. If they don’t make changes that are observable outside the loop, they can be assumed to terminate. ThTs a compile-time thing. https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1509.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1509.pdf (not guaranteed to be equal to the final wording of the standard): “An iteration statement that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate.” Rationale for including that: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1528.htm (basically, without it compilers can’t optimize loops that terminate as well as with it)
- AstralStorm 4y agoWith some exceptions. In C++ infinite loops doing IO, accessing volatile variables or atomics are not undefined. Hence for(;;) std::this_thread::sleep_for(1s) is not UB. C is even more lenient.