Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!nike!ucbcad!ucbvax!hplabs!tektronix!reed!bart From: bart@reed.UUCP (Bart Massey) Newsgroups: net.micro.mac Subject: Re: Megamax bug? Message-ID: <3687@reed.UUCP> Date: Wed, 18-Jun-86 12:08:13 EDT Article-I.D.: reed.3687 Posted: Wed Jun 18 12:08:13 1986 Date-Received: Sat, 21-Jun-86 10:06:36 EDT References: <1023@k.cs.cmu.edu> <219@analog.UUCP> Reply-To: bart@reed.UUCP (Bart Massey) Distribution: net Organization: Reed College, Portland, Oregon Lines: 66 In article <219@analog.UUCP> kim@analog.UUCP (Kim Helliwell) writes: >Greg Stein at CMU said... > > I'm working in Megamax C, and I noticed a problem while I was in the > > debugger. It comes from these statements: > > > > for (i = ntables; i--; ) > > if (i == -1) > > > > I would expect this to get the value of i, decrement the stored value, > > and then exit or execute the statement. Rather, it tests the value > > before it decrements, and the loop exits with i = 0. Is this a bug > > in the code generation, or is C supposed to do this? > > It's not a MegaMax bug, it's a Greg bug (:-). You have the i-- in the > slot that is supposed to be the termination condition--so your description > sounds like exactly what I would expect to happen. If you want the i-- > to decrement i without testing it for equality to 0 first, it has to > be after the second semicolon: > > for (i = ntables; ; i--) > if (i = -1) It may be that you have to do this, (BTW, you almost certainly meant "for(i = ntables; i; i--) ") but it's not a "Greg Bug", it's a Megamax bug. Here's a copy of what I wrote Greg, which he probably didn't receive, because of mailer problems... (Sorry, Greg). K&R specify (page 202) that for( i = ntables; i--; ) should be equivalent to i = ntables; while( i-- ) They also specify that "When postfix -- is applied to an lvalue the result is the value of the object referred to by the lvalue. After the result is noted, the object is incremented in the same manner as for the prefix -- operator." (page 187) Putting this all together... Imagine that ntables is 37. The while statement will clearly decrement "i" down to one. Then what will happen? Answer: one is boolean true, so "i" will be decremented to zero. The statement will be executed. zero is boolean false. So the statement won't be executed. BUT "I" WILL STILL BE DECREMENTED, as a result of evaluating "i--". Thus, the will be executed 37 times, (with i = 37 downto 1), and "i" will end up with the value -1. As a cross check, our 4.2BSD C compiler (reasonably close to K&R), when fed the original "for" statement, does indeed set "i" to -1. The Megamax C compiler is probably putting the branch on the wrong side of the decrement. Understandable (perhaps especially so to Greg, who co-authored the original Rascal compiler), but wrong. Sorry for the length of this posting, but it would have been hard for me to say it shorter... Bart Massey ..tektronix!reed!bart