We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I AM NOT UNDERSTANDING THE SITUATION WHEN SHOULD I USE "VOLATILE" WITH VARIABLES ???
Another thing: how can the values of those variables be observed? They can only be observed through the actions of that same program.
This argument, essentially a subset of the "as-if" rule, holds for normal variables, but not for volatile-qualified ones. Forbidding application of the as-if rule is exactly what volatile is about. Following your line of reasoning, volatile might as well imply static duration, since it doesn't have any effect on automatic variables.
Also note C99 5.1.2.3: writes to volatile objects are side-effects, and as such they have to be completed before the next sequence point. I rather much doubt that the seeming loop-hole 6.7.3p6 about implementation-definedness of "access to a volatile object" should be understood to cover writing a value to it. Particularly as it stands after a clause of the pattern "except ... as mentioned previously"
It seems to depend a lot on gcc version. Most probably, only versions before 4.x keep empty loops.
Test with empty loop and -O3:
gcc 2.95.4 leaves loop. gcc 3.0.4 leaves loop. gcc 3.3.1 leaves loop. gcc 3.3.5 leaves loop. gcc 3.3.6 leaves loop. gcc 4.1.2 strips loop.