🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1928

Re: A question on volatile accesses

#1928
From: Stephen Clamage
Date: Sat, 03 Nov 1990 17:16
48 lines
1835 bytes
rfg@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@taumet.com
Technical Details
Message-ID: <501@taumet.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!taumet!steve
References: <2388@lupine.NCD.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!taumet!steve
From: steve@taumet.com (Stephen Clamage)
Newsgroups: comp.std.c
Subject: Re: A question on volatile accesses
Message-ID: <501@taumet.com>
Date: 3 Nov 90 17:16:32 GMT
References: <2388@lupine.NCD.COM>
Organization: Taumetric Corporation, San Diego
Lines: 48