Caveats
12-5
12
Volatile objects are also presumed to have been changed in unknowable
ways between such points.
Here is an example of a program that fails because of a memory reference
that needs to be made volatile:
fiddle.c:
#define MY_PORT *((int *) 0x10000)
...
int read_my_port ()
{ return MY_PORT;
}
faddle.c:
...
while (read_my_port() == 0)
/* do nothing */;
ok_go_do_something ();
This program is incorrect, but it functions as intended when compiled with
compilers that do not attempt inlining across
.c files.
When these two files are compiled with global inlining, the compiler
translates the program to:
(1)
while (MY_PORT == 0)
/* do nothing */;
ok_go_do_something ();
And, since MY_PORT appears to be loop invariant (because it isn’t volatile),
we then get:
(2)
t = MY_PORT;
while (t == 0)
;
which loops forever if the first value read from *0x1000 is 0.