Re: De-referencing NULL



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"
.



Relevant Pages

  • Re: (part 10) More Schildt-like errors in Dicky Heathens book
    ... like mallocif mallocreturns a non-NULL pointer. ... If one is using a debugging malloc which checks that all accesses are ... when sz is zero, it gives the debugging allocator an opportunity to ...
    (comp.lang.c)
  • Re: Triplet 630APL meter movement adjustment
    ... was the same whether the meter was standind up or laying down. ... mechanical zero with the unit in the vertical position and took a ... There are three weights on the bottom of the meter pointer; ...
    (sci.electronics.repair)
  • Re: (MS-)DOS PC on a microcontroller??
    ... memory block of zero bytes. ... The pointer returned if the ... Each such allocation shall yield a pointer to ... support malloc/calloc requests for 0 bytes and whether or not the ...
    (comp.arch.embedded)
  • Re: Apple III Business BASIC Invokables
    ... I'm using Apple II Pascal 1.3 to assemble to a Pascal object module, then transfer to a physical /// disk... ... It pulls this off with extra hardware to snoop zero page address read/writes used by indirect indexed addressing mode instructions. ... When you get a 16 bit pointer to a buffer, it appears that you have to determine if it is an absolute pointer to somewhere in the current 64K, or an indirect pointer that has been already set up in the zero page/X-byte page. ...
    (comp.sys.apple2.programmer)
  • Re: (MS-)DOS PC on a microcontroller??
    ... memory block of zero bytes. ... all of malloc, realloc, and calloc and thus is isolated in the ... The pointer returned if the ... Each such allocation shall yield a pointer to ...
    (comp.arch.embedded)