3 ms·
If the loop is so useless, why is it in the code? Probably because it isn't useless. Hence the compiler should not optimize it.
by aap_ 8y ago
If the loop is so useless, why is it in the code? Probably because it isn't useless. Hence the compiler should not optimize it.
- colanderman 8y ago> If the loop is so useless, why is it in the code? Because perhaps it contains a body that optimizes away based on conditions out of control of the programmer? This happens all the time with macros/templates, and with platform-agnostic code. Only the compiler can resolve what's in the body; I want to trust the compiler to remove the loop if it is useless.
- tagrun 8y agoThat kind of empty loops are actually used for delays, waiting on interrupts to kick in etc. in embedded systems, where you typically fight against the compiler using volatile keyword. Example from https://www.coranac.com/tonc/text/video.htm https://www.coranac.com/tonc/text/video.htm: #define REG_VCOUNT *(volatile u16*)0x04000006 while(REG_VCOUNT < 160);
- jerrysievert 8y agoI'm curious as to if there's a tool that can map the sections of code that are optimized away by the compiler, and feed that back to the developer; thus code like this: for (int a = 0; a < 10000; a++); would emit a message at compile time allowing the human to take an additional look at the code and determine its usefulness. ultimately the code would be removed or refactored just to stop the nagging.