Subject: Re: NULL pointer return values
To: None <>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-kern
Date: 04/23/2002 23:58:20

>> Assuming that the bit pattern stored in a pointer storing a "null
>> pointer" is all-bits-zero is incorrect.  Assuming that "p = 0" sets
>> p to a null pointer is explicitly sanctioned by the standard.

> If on an arch which didn;t use 0 for a NULL pointer, and compiler
> allowed to internally assign NULL if we assign constant 0 to a
> pointer,

Not only allowed to but _required_ to, since we're talking about C.

> how would one address memory at 0x000000000,

This is architecture-dependent.  You may be able to do it with
something like ((char *)1)-1.  The issue may not even come up because
there not _be_ any "address 0".  Or it may be that a pointer is not
just a memory address - for example, on a machine with tagged pointers,
you would access memory at address zero by constructing a pointer with
zero in its address bits and something suitable (which may or may not
be all zero bits) in its tag bits.  (How you do this is, of course,
architecture- and maybe compiler-dependent.)  Or, as on most even
vaguely modern machines, there may be an address zero and pointers may
be just memory addresses, but there is no issue as far as C is
concerned because the compiler and/or the OS ensure that no valid C
object can occupy address zero - and if you really want to access
address zero, you do it by dereferencing a null pointer.  The language
doesn't guarantee that a null pointer doesn't point anywhere, only that
it doesn't point to any C language entity.  Dereferencing it can get
you anything or nothing - it can give you memory at address zero, it
can give you memory somewhere else, it can give you a fatal trap, it
can halt the machine, it can melt the CPU down into a puddle of liquid
silicon, it can even (at least as far as the C language is concerned)
it can make demons fly out of your nose.  (Yes, I cribbed that last.)

> unless
> long *ptr = 0x00000000;
> is actually not considered constant 0

No, 0x00000000 is an integer constant expression with value zero and
hence (in a pointer context, as it is in your example) is a perfectly
good null pointer constant.


/~\ The ASCII				der Mouse
\ / Ribbon Campaign
 X  Against HTML
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B