negative addresses

Henry Spencer henry at utzoo.uucp
Fri May 13 02:29:06 AEST 1988


> Your program's virtual address space probably starts at -2**31,
> rather than 0 (to give you the full range).  This means that C-language
> null pointers, because they are defined never to point to a valid
> address, will probably have to be something other than the standard
> all-zero bit representation.  This is not a real problem...

Unfortunately, it is a real problem, because there are zillions of
programs that implicitly assume that pointers are all-zeros.  It is
true that the language does not require it, but doing anything else
is an enormous pain in practice, according to the people who have
experimented with the idea.

Fortunately the problem can be bypassed, because there is absolutely no
reason why the null pointer has to point to the beginning of your address
space.  It is sufficient for the machine and the memory allocator to
conspire to ensure that no user data is ever allocated at location 0.
This would qualify as a nuisance, but hardly a disaster.

> ... it complicates the compiler somewhat (having to detect
> the assignment/comparison of a pointer and the integer constant 0 as a
> special case)...

Not by much.  Some machines already need such complications because their
pointers and integers are not the same size.

> ... uninitialized static pointers might break in horrible ways (this
> is not necessarily bad! ;-)

Are you going to fix all the programs that rely on it? ;-)  More to the
point, this is not an issue, because uninitialized static variables are
*not* initialized to all-zeros, they are initialized to the zero value
of their data type, which means the null pointer for pointers.  Now this
would be a bit of a pain for compilers on machines with odd representations
of the null pointer.
-- 
NASA is to spaceflight as            |  Henry Spencer @ U of Toronto Zoology
the Post Office is to mail.          | {ihnp4,decvax,uunet!mnetor}!utzoo!henry



More information about the Comp.lang.c mailing list