From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1d321b3a6b8bcab2 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-31 14:22:05 PST Path: nntp.gmd.de!newsserver.jvnc.net!news.cac.psu.edu!news.pop.psu.edu!hudson.lm.com!godot.cc.duq.edu!news.duke.edu!news.mathworks.com!uunet!gwu.edu!seas.gwu.edu!dobrien From: dobrien@seas.gwu.edu (David O'Brien) Newsgroups: comp.lang.ada Subject: Re: "Subtract C, add Ada" Date: 31 Jan 1995 22:22:05 GMT Organization: George Washington University Message-ID: <3gmd6d$mbf@cronkite.seas.gwu.edu> References: <3fo2ot$su2@miranda.gmrc.gecm.com> <3fode9$dap@soleil.uvsq.fr> <3g1shu$ksc@rational.rational.com> <3gdro9$aud@spam.csv.warwick.ac.uk> <3ghtke$9jc@felix.seas.gwu.edu> NNTP-Posting-Host: 128.164.9.3 X-Newsreader: TIN [version 1.2 PL2] Date: 1995-01-31T22:22:05+00:00 List-Id: Michael Feldman (mfeldman@seas.gwu.edu) wrote: : In article <3gdro9$aud@spam.csv.warwick.ac.uk>, : Jules wrote: : >Of course the correct version (and the version I still prefer) is: : > : >while (a[i++]!=b && i < ARRAYLIMIT); : Hmmm - I think I learned somewhere that the && is short-circuit in C. : So wouldn't you want to switch the two conditions, i.e. check the : limit _before_ incrementing that index? (It's late at night; this : comment may be off the wall.) Switching is required, and it would use the short-circuit idiom we love so much in C. But, the reason is that you skip that last element. Say we have: #define ARRAYLIMIT 3 foo a[ ARRAYLIMIT ]; When i == 2, the access ``a[ i++ ] != b'' is ok. However when we get to ``i < ARRAYLIMIT'', i == 3, and thus the statement is false and you stop the loop for the wrong reason. ``i <= ARRAYLIMIT'' would be needed, but it would cause you to access one element off the end of the array which is undefined. ANSI-C only says the *address* that points to one element beyond the end of an array must be valid. But accessing it is undefined behavior. [ANSI-C 3.3.6] Assuming we had: foo a[ ARRARYLIMIT + 1 ]; /* also very common way */ Doesn't help either, you also stop early. -- David (dobrien@seas.gwu.edu)