4 ms·
It means that empty loops (loops with empty bodies) can be completely removed if the controlling expression has no side effects. > This is intended to allow co
by pcvarmint 10y ago
It means that empty loops (loops with empty bodies) can be completely removed if the controlling expression has no side effects.
> This is intended to allow compiler transformations such as removal of empty loops even when termination cannot be proven.
It means while(i) {} can be eliminated as if i were 0, because there are no side effects in the loop expression or the loop body, and what would be the point of the loop if it never terminated on a non-constant expression?
As an optimization, the optimizer is allowed to eliminate it as a useless loop with no side effects. If you really want an infinite loop, you can use while (1) {}.
There are cases where automatically generated C code might have empty loops which are useless.
If you really want to go to sleep, use pause() or similar. An infinite loop eats up CPU cycles.
- rkv 10y ago> If you really want to go to sleep, use pause() or similar. An infinite loop eats up CPU cycles Yes but an infinite loop + sleep is okay, right?
- dfox 10y agoFrom compiler's POW call to sleep() is side-effect.
- TorKlingberg 10y agoIt's quite common in embedded systems to have the fault handler end with an infinite loop, to give the programmer a chance to attach a debugger an inspect the call stack. Sometimes this behavior is turned on or off with a debug flag, which can trigger this unexpected optimization if the flag is not a compile time constant.
- planteen 10y agoYou could also add a volatile declaration
- boomlinde 10y agoYou could always say if (flag) while (1);
- TorKlingberg 10y agoYes, that would make more sense. I have never seen this optimization actually bite anyone.
- pcvarmint 10y agoA halt instruction (HLT on x86) might also work.