In this example, does correctness require global_value
to be declared volatile
?
int global_value = 0;
void foo () {
++ global_v
No, the volatile
keyword is not necessary here. Since global_value
is visible outside the function bar
, the compiler must not assume that it remains the same if another function is called.
[Update 2011-07-28] I found a nice citation that proves it all. It's in ISO C99, 5.1.2.3p2, which I am too lazy to copy here in its entirety. It says:
At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.
Sequence points include:
There you have your proof.