Article View: net.micro.mac
Article #6061Re: Megamax bug?
From: matt
Date: Mon, 16 Jun 1986 04:13
Date: Mon, 16 Jun 1986 04:13
49 lines
2546 bytes
2546 bytes
In article <1436@fisher.UUCP>, njt@fisher.UUCP (Nathaniel Thurston) writes:
- In article <219@analog.UUCP> kim@analog.UUCP (Kim Helliwell) writes:
- >> for (i = ntables; i--; ) <statement>
- >> if (i == -1) <more statements>
- >>
- >> 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?
- >>
- >> Greg
- >
- > for (i = ntables; ; i--) <statement>
- > if (i = -1) <more statements>
- >
- >Kim Helliwell
-
- You're both wrong. The first loop should perform the action Greg describes,
- using the value before decrementing. To have C test the value after
- decrementing, use --i. In the "fix" that Kim gives, the loop will never
- exit without a break statement.
- The correct code is:
- for (i = ntables; --i; ) <statement>
- if (i == -1) <more statements>
- --
-
I believe Greg is right. When the loop falls through, `i' should end
up as -1. If in fact it does not, the C compiler forgot to generate
the trailing DEC instruction on that branch of the test. This seems
serious, since checking a post-condition like this is fairly common in
C; are you sure?
Kim's code is wrong, and would never exit (empty condition == true).
Also the trailing test has an assignment (i = -1) instead of a
conditional (i == -1). Thus, that test would always succeed.
Nathaniel's code is wrong, since I presume Greg intended to execute the
<more statements> line if the loop fell through (vs. a `break');
these statements would not be executed if the loop fell through at
i==0. The correct condition here (if it were the right test, which it
ISN'T for traversing arrays) would be `if (i == 0) ...'.
Sigh.
- Matt
-------
UUCP: {ucbvax,ihnp4,randvax,trwrb!trwspp,ism780}!ucla-cs!matt
ARPA: matt@LOCUS.UCLA.EDU
Technical Details
Message-ID:
<15824@ucla-cs.ARPA>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!pyramid!pesnta!hplabs!sdcrdcf!ucla-cs!mattReferences:
<1023@k.cs.cmu.edu> <219@analog.UUCP> <1436@fisher.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!pyramid!pesnta!hplabs!sdcrdcf!ucla-cs!matt From: matt@ucla-cs.UUCP Newsgroups: net.micro.mac Subject: Re: Megamax bug? Message-ID: <15824@ucla-cs.ARPA> Date: Mon, 16-Jun-86 00:13:56 EDT Article-I.D.: ucla-cs.15824 Posted: Mon Jun 16 00:13:56 1986 Date-Received: Tue, 17-Jun-86 11:51:18 EDT References: <1023@k.cs.cmu.edu> <219@analog.UUCP> <1436@fisher.UUCP> Distribution: net Organization: UCLA CS Department Lines: 49