Does your compiler get this program right?
Jim Meyering
meyering at cs.utexas.edu
Thu Nov 24 09:29:47 AEST 1988
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).
You might be interested to know that while the Sun3/os3.2
(or an HP, don't remember which) C compiler produced code
that gave the "correct" results for your code, when I replaced
that statement by the two:
*f += *g++; or *f = *f + *g++;
f++; f++;
I found that the size of the object code was actually reduced.
Chalk one up for readability *and* efficiency.
More information about the Comp.lang.c
mailing list