Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!uwvax!oddjob!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Query: Implementation with non-zero NULL Message-ID: <6877@mimsy.UUCP> Date: Mon, 1-Jun-87 23:33:31 EDT Article-I.D.: mimsy.6877 Posted: Mon Jun 1 23:33:31 1987 Date-Received: Wed, 3-Jun-87 04:20:06 EDT References: <158@delftcc.UUCP> <3673@gitpyr.gatech.EDU> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 63 Keywords: C, NULL, portability In article <3673@gitpyr.gatech.EDU> allen@gitpyr.gatech.EDU (P. Allen Jensen) writes: >On the PRIME system under PRIMIX, there is a special "undefined pointer" >value. ... Because of this, constructs such as the > > if(p) > >do not yield correct results. Then the PRIMIX compiler is broken. See K&R, p. 201. (This does not, of course, help those who must use the broken compiler.) >The value of NULL is 0x60000000 on a prime. If NULL is `#define'd as 0x60000000, the compiler is even more broken. Given this special nil pointer format, and assuming that the union below gets at the proper portion of a pointer, the program below should print only `right's: #include #define magic 0x60000000 /*ARGSUSED*/ main(argc, argv) int argc; char **argv; { union { long l; int *p; } u; static char r[] = "right\n"; static char w[] = "wrong\n"; u.p = 0; printf(u.l == magic ? r : w); printf(u.p == NULL ? r : w); printf(u.p ? w : r); exit(0); } The compiler is required to transform any integer constant zero in any pointer context into the appropriately typed nil pointer. In the expressions `if (p) s1; else s2;' and `p ? e1 : e2', where p is a pointer, p is being implicitly compared to a constant zero; if p is not equal to the nil pointer resulting from this transformation, the compiler should evaluate the `true' branch or expression, s1 or e1; otherwise, it should evaluate the `false' branch or expression, s2 or e2. If the compiler does something else, it is broken. Given these conversion rules, the only correct ways to define NULL are #define NULL 0 and #define NULL 0L /* or 0l, or ((long)0) */ The latter is unnecessary and potentially troublesome. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris