A question on volatile accesses

Chris Torek chris at mimsy.umd.edu
Sun Nov 4 06:29:00 AEST 1990


In article <2388 at lupine.NCD.COM> rfg at lupine.ncd.com (Ron Guilmette) writes:
>Given ... volatile int *ip; ... i = *++ip;
>I'd like to know if the standard allows the incrementation of `ip'
>to occur *after* the volatile access.

Yes.

>In other words, could the program above legally be treated as:
>... i = *ip; ++ip; ...

No.  The equivalent expansion is, instead,

	i = ip[1]; ++ip;
	/* or: tmp = ip + 1; i = *tmp; ip = tmp; */

Even if `ip' itself were volatile (`volatile int *volatile ip;') the
same sequence could be used, since `volatile' really does not say much
anyway.  The main idea behind `volatile' is to make sure that loads
and stores are not deferred beyond sequence points.  Without volatile,
the sequence

	i = *ip; ip++; i *= *ip;

could be computed as

	x = ip[1]; y = ip[0]; i = x * y; ip += 2;

whereas with it, the x and y above must be loaded in the other order.
I am not at all certain whether ip must be altered between those
operations (if ip itself were volatile, this would be true).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.std.c mailing list