volatile registers (was: The world is not ready for 'volatile')

Dennis L. Mumaugh dlm at cuuxb.ATT.COM
Wed Jan 4 13:52:18 AEST 1989


In      article      <9316 at ihlpb.ATT.COM>       nevin1 at ihlpb.UUCP
(55528-Liber,N.J.)  makes  some comments about volatile that seem
to reflect many people's understanding.

The concept "volatile" should not occur in any general C program.
This  feature  exists only for the stand alone, the real time and
the kernel hacker.

Its purpose is to state that each  and  every  reference  to  the
volatile  object  [location, structure, register, or whatever] is
necessary and not to optimize such references in any way.

Its only use in a UNIX environment *should* be  with  respect  to
shared  memory  or  mmaped files where more than once process may
access the same location "simultaneously".  I have seen its  need
only  in  writing  drivers  where  a  location actually maps to a
device register and each  referrence  to  the  location  actually
results in the device doing something.  For example:

volatile int foo;
...
foo = 'H';
foo = 'e';
...
foo = 'd';
foo = '.';

might print "Hello world." on the console of the computer if  the
location  "foo"  were  the  output  port for a device controller.
Without the volatile the compiler or the optimizer is allowed  to
reduce the above to

	foo = '.';

In this sense the constuct of a "volatile" register doesn't  make
sense  in the C language as it is not possible to bind a variable
to any specific register [in the instruction set sense].

The question of aliasing is orthoganol to volatile.  It  just  so
happens  that  the  side-effects of declaring a volatile location
result in anti-aliasing of pointers to volatile locations.
-- 
=Dennis L. Mumaugh
 Lisle, IL       ...!{att,lll-crg}!cuuxb!dlm  OR cuuxb!dlm at arpa.att.com



More information about the Comp.std.c mailing list