Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!ames!amdahl!pyramid!thirdi!peter From: peter@thirdi.UUCP (Peter Rowell) Newsgroups: comp.lang.c Subject: Re: pointer debate raging on... Message-ID: <31@thirdi.UUCP> Date: Mon, 27-Apr-87 16:03:26 EDT Article-I.D.: thirdi.31 Posted: Mon Apr 27 16:03:26 1987 Date-Received: Tue, 28-Apr-87 03:38:12 EDT References: <149@sds.UUCP> Reply-To: peter@thirdi.UUCP (Peter Rowell) Followup-To: comp.lang.c Organization: Third Eye Software, Palo Alto, CA Lines: 43 Keywords: pointers, religion, sin, Ritchie, razor blades, etc. Distribution: Having watched this for a while, I feel the need to comment. The following should be read in the context of the *real* world of *real* C compilers. Although ANSI may someday save us from our sins, it ain't doing it yet. Anyone who is trying to create code that will work on a broad spectrum of machine/os/compiler combinations must necessarily use coding paranoia sufficient for the worst case. Although it is correct that the comparison (charptr == 0) is guaranteed to work by the Holy Writ (aka K&R), all other bets are off as you stray from the homogenized, pasteurized world of the VAXen. As pointed out, environments where sizeof(int) != sizeof(char *) require NULL to be define'd as ((char *)0) (or some variation on that theme). Unfortunately, this is not the end of this train of thought. There is NO guarantee I am aware of that sizeof(char *) == sizeof(struct foo *)! For this reason (and to adhere to a local naming convention), we use a specifically typed nil pointer for each and every type. For example, #define fooNil ((struct FOO *)0) #define sbNil ((char *)0) /* string of bytes (aka ASCII) */ #define pbNil ((unsigned char *)0) /* pointer to arbitrary bytes */ etc. Since we always have a "correct" nil for everything, we always pass exactly the correct value. True, this *almost* always means that the same 32 bit (16 bit/48 bit/64 bit) "0" is being passed, but it saves you should some compiler person become excessively clever. It also means that your comparisons are of the form: if (foo != fooNil) ... Which is clearly correct, vs. if (foo != NULL) ... Which just doesn't feel safe to me. This method has stood us in good stead (except when we have foolishly violated it) in porting a source-level debugger to over 70 different systems - not all of which were done by followers of the True Faith. Peter Rowell Third Eye Software (415) 321-0967