🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #5827

Re: null pointers of various types.

#5827
From: Henry Spencer
Date: Mon, 27 Mar 1989 20:07
59 lines
3290 bytes
In article <7672@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes:
>I recently made the following assertions about pANSI C.
>Are they correct?
>
>* The number zero (0) compares equal with any null pointer of any type.

The integer *constant* 0, an integer *constant* expression that evaluates
to 0, or either of those cast to "void *", compares equal with any 
null pointer.  Comparing a general number, e.g. an int variable, to a
pointer is illegal.  And there is no guarantee that casting a general
zero value to a pointer will produce a null pointer.

The real problem here is that C has the notion of a null pointer constant,
which is a magic value of pointer type, but instead of writing it as "nil"
like some other languages do, C says that an integer constant with the
value zero, when found in a pointer context, means null.  The transformation
into the magic pointer value is done at compile time, not at run time, so
just any zero won't do -- it has to be something that the compiler knows
to be zero.  It's just an odd way of writing the null pointer *constant*;
it does not imply any general relationship between integer zeros and null
pointers.

>* The bit storage of a null pointer is not necessarily all zeros.

Correct.

>* Null pointers of two types do not necessarily compare equal.

Comparing two pointers of different types is, in general, illegal.
The results of casting a pointer of one type to a different type is,
in general, implementation-defined, but there is a specific exemption
for null pointers.  Any two null pointers, when cast to pointer types
that are sufficiently similar for comparison to be legal, compare equal.

>* A null pointer of one type, cast in to another pointer type, will
>  always be cast to a null pointer in the destination type.

I do not actually find this to be guaranteed in the general case,
although the requirement that it compare equal to a null pointer of
the destination type will convince most implementors to make it so.
The two properties of null pointers are that they compare equal to
each other, and that they compare unequal to a pointer to any object
or function (subject, in all cases, to the requirement that types be
similar enough for comparison to be legal).  The wording of 3.2.2.3
guarantees that casting to other pointer types preserves the first
property, but the draft is silent about the effects of general pointer
casts on the second property.

(Note that a null pointer constant does not become a null pointer until
it is converted to a specific pointer type, so *that* transformation
is exempt from this.)

(Note also that some specific pointer casts carry stronger guarantees.)

(The above is based on the Oct draft, but is probably correct for more
recent versions too.)
-- 
Welcome to Mars!  Your         |     Henry Spencer at U of Toronto Zoology
passport and visa, comrade?    | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
Technical Details
Message-ID: <1989Mar27.200731.20641@utzoo.uucp>
Path: arc1-ca.pugleaf.net!utzoo!henry
References: <7672@june.cs.washington.edu>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: null pointers of various types.
Message-ID: <1989Mar27.200731.20641@utzoo.uucp>
Organization: U of Toronto Zoology
References: <7672@june.cs.washington.edu>
Date: Mon, 27 Mar 89 20:07:31 GMT