icky C code

chris at umcp-cs.UUCP chris at umcp-cs.UUCP
Thu Jul 17 10:17:32 AEST 1986


In article <2239 at brl-smoke.ARPA> mangoe at mimsy.umd.edu (Charley Wingate) writes:
>... I've been looking at 4.3 code, particularly for the Sun.  There
>are plenty of places where pointers are used indifferently as
>pointers to structures and as pointers to arrays.

If you mean `pointers to structures and pointers to arrays of those same
structures', there is nothing wrong with that.  The C memory model demands
that an array be a linear arrangement of its components.  If this is not
true on a machine, that machine does not run C code.

>There is code which relies on ints and pointers being the same size.

Alas, this seems to creep in everywhere---usually as a result of
programmer laziness.  An imagined programming session:

	/* Oops, gotta call getenv, but the declarations are all
	   way above.  Enh, I won't bother declaring it. */
	p = (char *) getenv("FOO");

First, this is bad coding, like writing unnecessary LOOPHOLEs in Mesa.
Second, lint will complain.  Third, it is not really all that hard to
go put in the declaration.  Leave out the cast and even the compiler
will complain, which will remind you to go clean up!

>There is code which relies on successive declarations being stored
>contiguously and in order.

Where?

>Code which relies on NULL equalling zero is omnipresent.

But NULL *must* equal zero.  If you mean `the bit pattern of the
null pointer-to-X equalling the bit pattern of the zero integer',
again, this is bad coding, and again, lint will complain.

>All this leads me inescapably to the conclusion that C was designed
>to allow programmers to violate the rules which everyone else is
>busily putting into their languages to protect themselves.

In 1978 (or perhaps earlier, but the documentation says July 1978),
Steven C. Johnson recognised the need for a type-checker for the
C language.  He wrote one.  It is called lint.  If you think
type-checking is good, *use it*.  If you just want your program
to work on your Widget-brand computer, do whatever you like---
though you might still find lint useful.

There are those who consider `lint' a necessary part of the C
compiler, that the proper compilation command is

	lint file.c; cc file.c -o file

These people never seem to worry about all the problems you describe.
The C language has a good type-checker.  It is just not built into
the `normal' compiler.  This may be a serious problem to some, but
I have never found it to be so.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list