Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utcs!mnetor!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP Newsgroups: net.lang.c Subject: Re: icky C code Message-ID: <2446@umcp-cs.UUCP> Date: Wed, 16-Jul-86 20:17:32 EDT Article-I.D.: umcp-cs.2446 Posted: Wed Jul 16 20:17:32 1986 Date-Received: Wed, 16-Jul-86 23:12:36 EDT References: <2239@brl-smoke.ARPA> Reply-To: chris@maryland.UUCP (Chris Torek) Organization: University of Maryland, Dept. of Computer Sci. Lines: 59 In article <2239@brl-smoke.ARPA> mangoe@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@umcp-cs ARPA: chris@mimsy.umd.edu