Path: utzoo!attcan!uunet!husc6!bloom-beacon!apple!bionet!agate!ucbvax!hplabs!amdcad!crackle!tim From: tim@crackle.amd.com (Tim Olson) Newsgroups: comp.lang.c Subject: Re: Does your compiler get this program right? Message-ID: <23624@amdcad.AMD.COM> Date: 24 Nov 88 03:40:44 GMT References: <2298@cbnews.ATT.COM> <4082@cs.utexas.edu> Sender: news@amdcad.AMD.COM Reply-To: tim@crackle.amd.com (Tim Olson) Organization: Advanced Micro Devices, Inc. Sunnyvale CA Lines: 35 Summary: Expires: Sender: Followup-To: In article <4082@cs.utexas.edu> meyering@cs.utexas.edu (Jim Meyering) writes: | In article <2298@cbnews.ATT.COM> lvc@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@crackle.amd.com)