Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: $Revision: 1.6.2.16 $; site prism.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!prism!matt From: matt@prism.UUCP Newsgroups: net.lang.c Subject: Re: how has C bitten you? Message-ID: <5400010@prism.UUCP> Date: Tue, 30-Jul-85 11:00:00 EDT Article-I.D.: prism.5400010 Posted: Tue Jul 30 11:00:00 1985 Date-Received: Sat, 3-Aug-85 05:26:50 EDT References: <4051@alice.UUCP> Lines: 48 Nf-ID: #R:alice:-405100:prism:5400010:000:1845 Nf-From: prism!matt Jul 30 11:00:00 1985 > /* Written 8:36 pm Jul 25, 1985 by ark@alice in prism:net.lang.c */ > /* ---------- "how has C bitten you?" ---------- */ > > I am collecting examples of how C can bite the unwary user. > > Consider, for example, the following program fragment: > > int i, a[10]; > for (i = 0; i <= 10; i++) > a[i] = 0; > > On many implementations, this will result in an infinite loop. > > /* End of text from prism:net.lang.c */ This looks to me like it will simply overwrite one int's worth of memory beyond the end of the array "a" with the value 0. Granted, depending on what happens to be after "a", this can have disastrous results, but is there really an implementation in which it will (reliably) lead to infinte looping? On the other hand, in an implementation where char's are unsigned, this common construct WILL lead to an unterminating loop. I have been bitten by this several times porting code that assumed signed characters to implementation of C without them. char x; while (--x) { do anything... and then some... } I sure wish that while the ANSI committee was adding "signed" to the language, they had standardized whether the default for "char" was signed or unsigned. As long as compilers have to provide them both anyway, what's the harm in choosing one as the default? (Well, maybe the C programming community will eschew the use of "char" and always use either "signed char" or "unsigned char" as appropriate. Wanna bet?) ----------------------------------------------------------------------------- Matt Landau {cca, datacube, ihnp4, inmet, mit-eddie, wjh12}... Mirror Systems, Inc. ...mirror!prism!matt Cambridge, MA (617) 661-0777 ----------------------------------------------------------------------------- "Replace this mandolin with your wombat..."