K
Kenneth Brody
Keith said:The NULL macro "expands to an implementation-defined null pointer
constant" (C99 7.17p3).
The term "null pointer constant" is defined in C99 6.3.2.3p3:
An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer constant.
But, if the 6.3.2.3p3 definition of "null pointer constant" is "an
integer constant expression with the value 0 .. cast to type void *",
then why does 7.17p3 say that NULL "expand to an implementation-
defined null pointer constant"? If it must be zero, why say that is
is "implementation-defined"? Why not say it is "((void *)0)"?
Doesn't 6.3.2.3p3 say that zero, cast to void * is _a_ null pointer
constant? It doesn't say that no other void * can be a null pointer
constand, does it?
"((void*)&MyNullAddress)" doesn't qualify. Furthermore, a "null
pointer" (as distinct from a "null pointer constant") "is guaranteed
to compare unequal to a pointer to any object or function";
"((void*)&MyNullAddress)" fails on that count as well.
What if __MyNullAddress isn't a real object, but a magic name? Or
perhaps &__MyNullAddress doesn't point to any object. Perhaps it is
magically placed outside the address space of the current process.
Although C99 6.6p10 says:
An implementation may accept other forms of constant expressions.
But of course the conversion of the integer constant 0 to a pointer
type yields an implementation-specific pointer representation, and I
believe that representation can legitimately vary from one execution
of a program to another, as long as it's always treated as a null
pointer within each execution.
Note that the "%p" format used by fprintf() and fscanf() doesn't
restrict the implementation's flexibility here; the converted value is
required to work only during the same program exexution, and there's
no stated exception for null pointers.
Of course, I'm not saying that it necessarily makes sense to do
such strange things, but perhaps an all-bits-zero pointer is a valid
address as far as the hardware is concerned, and you wanted to make
sure that dereferencing NULL would trigger a hardware trap. Perhaps
the way to do that would be to have &__MyNullAddress evaluate to the
start of the next 4K boundary after the end of the initial stack
pointer?
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>