negative addresses

Stan Friesen sarima at gryphon.CTS.COM
Thu May 19 10:36:08 AEST 1988


In article <10001 at tekecs.TEK.COM> andrew at frip.gwd.tek.com (Andrew Klossner) writes:
>Doug Gwyn (gwyn at brl-smoke.ARPA) writes:
>
>> I don't think this is true.  How about an example?
[[Of code assuming the null pointer is all zero bits]]
>
>Sure Doug, from the system V kernel that you defend so ardently :-),
>file io/tt1.c (vanilla release 3.1):
>
>		if (tbuf->c_ptr)
>
>		if (tbuf->c_ptr == NULL)
>
>		if (tp->t_rbuf.c_ptr) {
>		if (tp->t_tbuf.c_ptr) {
>
>The C standards I've seen so far are pretty clear in stating that the
>conditional is compared against zero.  There doesn't seem to be leeway
>to define pointer comparisons to be against some non-zero NULL value.

	Yes, but they ALSO require that comparing a NULL-pointer to zero
evaluate to true *whatever* the representation of the NULL-pointer. The
compiler is *also* required to convert the integer constant 0 to the
NULL-pointer on assignment. So *none* of the above examples assume anything
about the representation of the NULL-pointer, they are all strictly
conforming. There *are* cases of code that does make such assumptions.
They all have the following general form:

func1(p)
char *p;
{
	/* stuff */
}

...

func2()
{
	...
	func1(0);
}

	In this example the code assumes both the representation *and* the
size of NULL-pointer. This code is *not* portable even among existing
compilers. Nor is it even conforming, let alone strictly so. Any code of
this form only works accidentally and needs to be fixed anyway.
-- 
Sarima Cardolandion			sarima at gryphon.CTS.COM
aka Stanley Friesen			rutgers!marque!gryphon!sarima
					Sherman Oaks, CA



More information about the Comp.lang.c mailing list