A question on volatile accesses

Stephen Clamage steve at taumet.com
Sun Nov 4 04:16:32 AEST 1990


rfg at lupine.ncd.com (Ron Guilmette) writes:

|	volatile int *ip;
|	void foobar ()
|	{
|		register int i;
|		do
|			i = *++ip;
|		while (i);
|	}
|I'd like to know if the standard allows the incrementation of `ip'
|to occur *after* the volatile access.
|In other words, could the program above legally be treated as:
|		do {
|			i = *ip;
|			++ip;
|		} while (i);
|This seems entirely counter-intutive to me, and yet one supposedly ANSI
|C compiler provides such a treatment.

You have changed the semantics here.  Forgetting volatile for the moment,
	 i = *++ip;
is in some sense equivalent to
	 ++ip;
	 i = *ip;
or to
	i = ip[1];
	++ip;
by the definition of pre-increment.

|I guess the question comes down to this: When you dereference a
|pre-incremented pointer, can you always safely assume that the
|pre-increment has already occured by the time the indirection
|through the pointer occurs?

There is no sequence point within the expression
	 i = *++ip;
The side effect of incrementing ip must occur before executing the
statement following the ";", and the expression must be evaluated
AS IF it occurred before dereferencing ip.

So yes, the expression must behave as though the increment occurred
before the dereference, but no, the increment need not actually
occur then.  Since ip is not itself volatile, as you pointed out,
this distinction should not make any difference.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.std.c mailing list