Re: De-referencing NULL
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Mon, 04 Aug 2008 08:43:31 -0700
santosh <santosh.k83@xxxxxxxxx> writes:
[...]
Conceptually a null pointer is distinct from a pointer containing the[...]
address value zero, but under many flat memory model architectures
their representations are identical and hence, in the absence of any
special interpretation (which becomes cumbersome), deferencing a null
pointer performs the same action as deferencing a pointer containing
address zero.
Sort of -- except that there's not really such a thing as "the address
value zero" in C, at least not in the sense that you mean.
You can *convert* an integer value zero to a pointer type. If the
converted value happens to be a constant expression, the result is a
null pointer value, due to the special-case rule that C uses to define
null pointer constants. If it's a non-constant expression whose
current run-time value is zero, the result of the conversion is
implementation-defined, and may or may not correspond to "the address
value zero" on the underlying system, assuming such a thing is even
meaningful.
This means that converting a value of zero to a pointer type might
yield different results depending on whether the zero value is a
constant expression or not, which is a bit bizarre. But most systems
don't make this distinction because, as santosh said in text I've
snipped, most systems choose to use all-bits-zero as the null pointer
representation, avoiding the need for special-case code to handle the
special-case rule for null pointer constants. On most (but not all)
such systems, attempts to dereference a null pointer are caught with
no additional effort, since 00000000 is not a valid address.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- References:
- De-referencing NULL
- From: rahul
- Re: De-referencing NULL
- From: santosh
- Re: De-referencing NULL
- From: santosh
- De-referencing NULL
- Prev by Date: Re: Font request
- Next by Date: Re: function calls, mandatory compiler promotion of int arguments to size_t?
- Previous by thread: Re: De-referencing NULL
- Next by thread: Re: De-referencing NULL
- Index(es):
Relevant Pages
|