Re: Null pointers

From: Christian Bau (christian.bau_at_cbau.freeserve.co.uk)
Date: 08/07/04


Date: Fri, 06 Aug 2004 23:42:34 +0100

In article <ln3c30twl2.fsf@nuthaus.mib.org>,
 Keith Thompson <kst-u@mib.org> wrote:

> Christian Bau <christian.bau@cbau.freeserve.co.uk> writes:
> [...]
> > Keep in mind that cast from int to char* is "implementation defined" and
> > doesn't necessarily give a useful result, so if you have a variable "int
> > x" then "(char *) x == NULL" could be true for many different values of
> > x.
> >
> > When you use a cast to convert say from int to float, the bits don't
> > stay unchanged: int i = 1; float f = (float) i; will produce very
> > different representations in i and f even though both are equal to one.
> > Same with a cast to convert from int to char*. Now typically compilers
> > where a null pointer has an all-bits-zero representation will leave the
> > representation unchanged, but if a null pointer has the same
> > representation as 0x12345678 then that would be no good.
>
> I'm going to use the notation $12345678$ to refer to a pointer whose
> internal representation is the same as the integer 0x12345678.
>
> Even an implementation with the null pointer represented as
> $12345678$, integer to pointer conversions could still leave the bits
> unchanged, except in the case of converting a null pointer constant to
> a pointer type. Remember that a null pointer constant is a source
> construct, and the conversion of a null pointer constant to a pointer
> value takes place during compilation.

No, it does not, at least not in C99.

For example, in the definition of "equality operator" it says

   Constraints: (three other possibilities and)
      One operand is a pointer and the other is a null pointer constant.

   Semantics: (two other possibilities and)
      Otherwise, at least one operand is a pointer. If one operand is a
null pointer constant, it is converted to the type of the other operand.

Conversion is done according to exactly the same rules as all other
conversions; since a null pointer constant is either of an integral type
or of type void*, that conversion is either a conversion from an
integral type to a pointer type or from void* to another pointer type.
But there is no difference between this conversion and any other
conversion. It is conceptually not a compile time construct. (Of course
an optimiser will determine the result of the conversion at compile
time, just as 2+3 will be determined at compile time by practically
every compiler).

> There's no requirement to
> duplicate that conversion at run time.
>
> So we could have:
>
> (char*)0 --> $12345678$ (null pointer)
> int zero = 0;
> (char*)zero --> $00000000$ (non-null pointer)
> (char*)0x12345678 --> $12345678$ (happens to be a null pointer)

Quite possible in C90, but most definitely not in C99. In C90, the
wording was such that in an assignment, or within an equality operator,
and probably some cases that I forgot, a null pointer constant was
replaced with a null pointer. (char*)0 was _not_ one if these cases and
in C90 not guaranteed to be a null pointer; in C99 they added that
_every_ conversion of a null pointer constant to a pointer produces a
null pointer.



Relevant Pages

  • Re: Draft standards OK for new compiler and libc?
    ... You said explicitly and incorrectly that moving ... have re-written it so that it would no longer compile with a K&R ... There is a much broader concept, that of a null pointer constant; ...
    (comp.std.c)
  • Re: Which compiler for a learner of C?
    ... Actually, while it looks nice enough, it doesn't compile my existing C ... Conversion from 'void*' to pointer to non-'void' requires an ...
    (comp.lang.c)
  • Re: 0, NULL and variadic functions
    ... and a null pointer constant at the same time (with or without ... null pointer constant, without having pointer type. ... The integer constant 0 is specifically of type int; ... the conversion is performed *by the assignment*. ...
    (comp.lang.c)
  • Re: NULL with representation other then all bits 0
    ... >> I believe this has been added as a requirement in C99. ... expression cast to type void *, is called a null pointer constant. ... It says what the result of a conversion from an integer type ...
    (comp.lang.c)
  • Re: NULL with representation other then all bits 0
    ... > expression cast to type void *, is called a null pointer constant. ... An integer may be converted to any pointer type. ... It says what the result of a conversion from an integer type ...
    (comp.lang.c)