Re: Definition of NULL

From: Keith Thompson (kst-u_at_mib.org)
Date: 11/16/04


Date: Tue, 16 Nov 2004 22:28:02 GMT

gordonb.5d4rz@burditt.org (Gordon Burditt) writes:
[...]
> The internal representation (bit pattern) of a NULL is not specified.
> It is *NOT* guaranteed to be 0xdeadbeef, not even on 32-bit machines.
> But if the internal representation is 0xdeadbeef,
> p == 0
> must be true if p contains a NULL pointer. If that means generating
> assembly-language code to compare p to the bit pattern 0xdeadbeef, so
> be it.
>
> However, neither
> memset((void *)&p, 0, sizeof(p));
> nor
> memset((void *)&p, 0xdeadbeef, sizeof(p));
> is guaranteed to set p to a NULL pointer.

The latter almost certainly won't, even if a null pointer is
represented as 0xdeadbeef. The second argument to memset() is
converted to unsigne char. On system with CHAR_BIT==8, this will most
likely (or certainly, but I'm too lazy to track the chapter(s) and
verse(s)) give you 0xef (the low-order 8 bits). If sizeof(p) is 4,
the value stored in p is going to be 0xefefefef.

(If CHAR_BIT==32, it should assign 0xdeadbeef to p, but that would be
an unusual system.)

Again, null pointers are explained quite well in section 5 of the
C FAQ (google "C FAQ"). There's little point in rehashing the
explanations here.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.


Relevant Pages

  • Re: How to know the memory pointed by a ptr is freed?
    ... > And what about the 37 copies made of that pointer after it was ... the value of "ptr" ... pattern stored in "ptr", or does it mean that we cannot under any ... that we gave that memory back to the system and any access to it ...
    (comp.lang.c)
  • Re: is NULL-checking redundant in accessor-functions?
    ... >> implementations of free don't, ... pattern apparently represents two different numbers? ... The answer lies in the *interpretation* of the bits. ... and the bits represent a pointer? ...
    (comp.lang.c)
  • Re: does a program work in all cases?
    ... standard promises is that it will *compare* equal to 0. ... NULL is a macro; in itself, it has no "internal representation". ... Most uses of the NULL macro will result in a null pointer value at run ...
    (comp.lang.c)
  • Re: General method for dynamically allocating memory for a string
    ... Allocated memory is initialized to a machine-specific bit pattern ... a pointer accidentally. ... even on machines with 32-bit pointers): ...
    (comp.lang.c)
  • Re: converting single to integer
    ... If by "different value" you mean a differing internal representation for the same numerical value... ... and interpret it as a Single, I sincerely doubt it would return the same value as that bit pattern interpreted as an Integer, which is what you are doing when you use memcopy to copy from a single to an integer. ... I think we're then talking past each other...yes, the internal bit pattern isn't the same between the same integer stored in Single as floating point as the same integer stored as a Long -- which is what I said. ... And, since there are only 23 bits reserved for the mantissa plus a "hidden" bit, any integer requiring over that many bits for representation will lose precision if cast to a Single. ...
    (microsoft.public.vb.general.discussion)

Loading