how has C bitten you?

matt at prism.UUCP matt at prism.UUCP
Wed Jul 31 01:00:00 AEST 1985


> /* Written  8:36 pm  Jul 25, 1985 by ark at 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..."



More information about the Comp.lang.c mailing list