I noticed on some code used through a circuit board code I'm using that there seems "busy waiting" in code I was given. for (i = 0; i < SOME_MACRO; i++);
Is a compiler technically allowed to completely optimize this out?
If not optimized, is trial-and-error the only way to tell how long the for loop will run for?
I think it is most likely that the programmer has a good familiarity with low-level assembly and knows the specifications of the hardware s/he is coding for, so they could have calculated it. Though less stellar answers are applicable too - it could be trial and error, it could be that a timing functionality exists but the programmer doesn't know about it, or it could even be a speed-up loop: http://thedailywtf.com/articles/The-Speedup-Loop
As LB said, as it is for specific controller, it is probably uses some intrinsic knowledge about it to calculate needed amount of iterations (by knowing controller frequency).
To avoid optimisating loop away completely, value in SOME_MACRO migh be volatile.
Funny link LB, but yes I think it's the former - the code is written for specific hardware so it runs at the right frequency. Thanks for the responses, didn't know about volatile.