Does your compiler get this program right?

Tim Olson tim at crackle.amd.com
Thu Nov 24 13:40:44 AEST 1988


In article <4082 at cs.utexas.edu> meyering at cs.utexas.edu (Jim Meyering) writes:
| In article <2298 at cbnews.ATT.COM> lvc at cbnews.ATT.COM (Lawrence V. Cipriani) writes:
| 	>A friend of mine found a bug in his C compiler.  He found
| 
| It's not a bug.
| 
| 	[...deleted commentary, code]
| 	>*f++ += *g++;		/* miscompiled line */
| 
| The standard does not specify the order of evaluation for such
| statements.  It's easier to see the ambiguity if you try to rewrite
| it without the += notation.  Which do you choose?
| 
|  1) *f++ = *f++ + *g++;
|  2) *f++ = *f + *g++;
|  3) *f = *f++ + *g++;
| 
| It can't be (1) since the side effect, f++, may be realized only once,
| but it's up to the compiler writer to choose between (2) and (3).

Your explination doesn't address the bug that Mr.  Cipriani points out
(which is that the side effect occured twice in some compilers), and I
don't think the "ambiguity" you refer to exists.  The semantics of
compound operators require that the lvalue (*f++ in this case) be
evaluated only once, with the result of that one evaluation being used on
both sides of the assignment.  The result of a postfix ++ operator is
the value of the operand (which is the result used on both sides of the
assignment). After the result is obtained, the operand is incremented. 
Therefore, the expression should behave like:

	*f = *f + *g++; f++;

	-- Tim Olson
	Advanced Micro Devices
	(tim at crackle.amd.com)



More information about the Comp.lang.c mailing list