4 ms·
Infinite loops are undefined behavior in C++.
by Kranar 4y ago
Infinite 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.