Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #1755

Re: null pointer constant, address constant

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.std.c
Subject Re: null pointer constant, address constant
Date 2012-12-18 19:12 -0500
Organization A noiseless patient Spider
Message-ID <kar0q1$3au$1@dont-email.me> (permalink)
References <kapqt2$es4$1@speranza.aioe.org> <kaq01g$6jp$1@dont-email.me> <kaqibr$gj3$1@speranza.aioe.org> <kaqmd8$u9m$1@dont-email.me> <lnzk1ark9h.fsf@nuthaus.mib.org>

Show all headers | View raw


On 12/18/2012 18:52, Keith Thompson wrote:
> Shao Miller <sha0.miller@gmail.com> writes:
> [...]
>> Possibly related, if we have:
>>
>>     int x = 42;
>>     x -= func_asking_user_for_number_and_they_provide_42();
>>     char * ptr = (char *) x;
>>
>> Does 'ptr' contain a null pointer value?
>
> Maybe.
>

And maybe not.

>>                                            We know that a null pointer
>> constant can be used to produce a null pointer value, but what about the
>> simple value 0 without the additional properties that an NPC has?  It
>> seems to me that it might be useful to distinguish between the two...
>
> The standard says that a constant arithmetic expression with the value
> 0, converted to a pointer type, yields a null pointer.  It does *not*
> say the same thing about a non-constant arithmetic expression with the
> value 0.
>

That's rather what I was suggesting to the original poster.

> Consider a system that represents null pointers as 0xFFFFFFFF
> (let's assume for simplicity that all pointer types have the same
> representation).  The standard requires `(char*)0` yields a null
> pointer, which means the conversion is non-trivial, though it takes
> place at compile time.  But any conversion that's *not* required
> to yield a null pointer can simply reinterpret the representation.
> So `(char*)zero`, where `zero` is a variable whose value is currently
> 0, would yield a pointer with an all-bits-zero representation,
> *not* a null pointer.  On the other hand, `(char*)0xFFFFFFFF`
> would (happen to) yield a null pointer value, though it's not a
> null pointer constant.
>

Good elaboration.  Precisely what I was thinking.  I am a tad worried 
that the original poster is confusing "null pointer value" with "the 
zero address in a linear address space," and casts of integers to 
pointers with "pointing to particular addresses in a linear address space."

> There are myriads of other possible implementations where `(char*)zero`
> does not yield a null pointer.  All one really needs to know is that the
> standard doesn't define the behavior; concrete (but hypothetical)
> examples can be useful in understanding why the standard does or does
> not make certain guarantees.
>
>> On x86 with BIOS, physical address 0 would be the first byte of the
>> Interrupt Vector Table.  If we wish to allow access to this as an
>> object, then surely we would want to distinguish that object from a null
>> pointer, which oughtn't to point to any object.
>
> A program that dereferences physical address 0 (which, BTW, is typically
> impossible in hosted implementations with virtual memory) most likely
> has undefined behavior.  It's perfectly valid for the result of that
> undefined behavior to be that it accesses the Interrupt Vector Table.
>
> [...]
>

Agreed.  My "surely" was intended to suggest a possible reason for _why_ 
the Standard makes it implementation-defined.  In free-standing 
boot-loaders like Syslinux, it's really rather useful for the undefined 
behaviour of _using_ a null pointer (even when constructed directly with 
a null pointer constant) to happen to do what you might desire on x86 
with BIOS*.

- Shao Miller

Off-topic post-script: *This is an example of "a real program that 
depends on undefined behaviour" (by Standard C, but obviously defined by 
the implementation)...  The very phrase that caused James K. to plonk 
me.  Oops! :)

Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-18 13:25 +0000
  Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 09:53 -0500
    Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-18 20:06 +0000
      Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 15:39 -0500
        Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-19 00:44 +0000
          Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 20:24 -0500
            Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-19 10:27 +0000
              Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-19 07:15 -0500
              Re: null pointer constant, address constant Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-21 03:03 +0200
                Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-21 12:13 -0500
                Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2013-01-09 00:44 +0000
                Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2013-01-08 20:21 -0500
                Re: null pointer constant, address constant Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-08 20:46 -0500
          Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 20:37 -0500
      Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 16:15 -0500
        Re: null pointer constant, address constant Keith Thompson <kst-u@mib.org> - 2012-12-18 15:52 -0800
          Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 19:12 -0500
            Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 19:51 -0500
          Re: null pointer constant, address constant Keith Thompson <kst-u@mib.org> - 2012-12-18 17:02 -0800
          Re: null pointer constant, address constant "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-12-20 05:34 -0800
            Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-20 10:33 -0500
              Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-20 12:28 -0500
                Re: null pointer constant, address constant Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 21:13 -0800
              Re: null pointer constant, address constant Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 21:05 -0800
            Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-20 12:17 -0500
            Re: null pointer constant, address constant Keith Thompson <kst-u@mib.org> - 2012-12-20 12:10 -0800
              Re: null pointer constant, address constant Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-20 21:25 -0800
  Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 14:02 -0500
    Re: null pointer constant, address constant Szabolcs Nagy <nsz@port70.net> - 2012-12-18 20:09 +0000
      Re: null pointer constant, address constant James Kuyper <jameskuyper@verizon.net> - 2012-12-18 15:54 -0500
        Re: null pointer constant, address constant Shao Miller <sha0.miller@gmail.com> - 2012-12-18 16:18 -0500

csiph-web