4 ms·
Is it actually true that endless loops are undefined behavior? This statement does not make sense at face value. I am aware that sometimes UB is used to throw a
by cjfd 5y ago
Is it actually true that endless loops are undefined behavior? This statement does not make sense at face value. I am aware that sometimes UB is used to throw away code, but is an endless loop really UB? Anyway, compiling while(true); in g++ with -O4 just yields a program that runs forerver....
- detaro 5y agoCompilers choose to not remove it in this case, probably because there is no clean way of making assumptions that provide a way out, unlike in the example from the link, but the core point is as I mentioned "making progress", specificall< the Progress guarantee (https://en.cppreference.com/w/cpp/language/memory_model#Progress_guarantee https://en.cppreference.com/w/cpp/language/memory_model#Prog...) In a valid C++ program, every thread eventually does one of the following: * terminate * makes a call to an I/O library function * performs an access through a volatile glvalue * performs an atomic operation or a synchronization operation It's fairly obvious how a loop that has none of these side-effects is thus a target to get turned into one that's guaranteed to terminate.
- gizmo686 5y agoAn infinite loop without any side effects.
- shusaku 5y agoOptimize our an infinite loop for infinite speed up!