Is something wrong with the compiler ?

Chris Torek chris at mimsy.umd.edu
Sat Oct 6 14:06:03 AEST 1990


In article <1903 at tuvie> hp at vmars.tuwien.ac.at (Peter Holzer) writes:
>Consider ... a = (a = a + 1) + 1
>This will compile (unoptimized) into:
>
>tmp1 = a + 1
>a = tmp1	*
>tmp2 = tmp1 + 1
>a = tmp2	*
>
>where the lines marked with * may or may not be deferred until the next
>sequence point....  [Second example deleted.] ... Any further comments?

Yes.  The ANSI standard appears% to allow the compiler to compile it
as

	tmp0 = a
	tmp1 = tmp0 + 1
	a = rand()	*
	tmp2 = tmp0 + 2
	a = tmp2	*

(using the same notation as above).  If the store of tmp2 happens
before the store of tmp1, a could wind up with a random value.

For a more realistic example, consider a machine that can do several
operations at once.  This machine might issue two separate
`simultaneous' operations, those being `increment a' and `set a=a+2'.
This could fetch the value of `a' after the increment has changed only
part of the ultimate value (assume it is a parallel machine with lots
of one-bit CPUs).  The end result is that dreaded word, `undefined'.

In short, when the ANSI standard says that the result of an operation
is undefined, it means UNDEFINED.  The computer can do anything (like
turn into a flower)---the system does not have to do anything remotely
reasonable.  When it says a result is implementation-defined, the
system can still do anything, but the product description has to tell
you what it does.

-----
% Note weasel word.  :-)
-- 
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.lang.c mailing list