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


Groups > comp.std.c > #1771

Re: null pointer constant, address constant

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.std.c
Subject Re: null pointer constant, address constant
Date 2012-12-20 12:10 -0800
Organization None to speak of
Message-ID <lnip7wqyc6.fsf@nuthaus.mib.org> (permalink)
References (1 earlier) <kaq01g$6jp$1@dont-email.me> <kaqibr$gj3$1@speranza.aioe.org> <kaqmd8$u9m$1@dont-email.me> <lnzk1ark9h.fsf@nuthaus.mib.org> <89afe21d-1a8b-4749-8ad1-ec98cc65a818@a8g2000vby.googlegroups.com>

Show all headers | View raw


"christian.bau" <christian.bau@cbau.wanadoo.co.uk> writes:
> On Dec 18, 11:52 pm, Keith Thompson <ks...@mib.org> wrote:
>> 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.
>
> It doesn't have to. The standard doesn't have to state something
> explicitly if it follows from other rules that are stated.

I agree with that, but it doesn't apply here.

> Converting _any_ integer to _any_ pointer gives an implementation-
> defined result.

Right (except that the result of converting a constant arithmetic
expression with the value 0 to a pointer is not implementation-defined).

>                 "implementation defined" means it is defined. So the
> implementation must define what you get if you write "int x = 0; char*
> p = (char *) x; char* q = (char *) ++x; char* r = (char *) ++x;".

Right.  Of course that definition can be given in very general terms.

> There is also the requirement for the special case that an integer is,
> by chance, a null pointer constant, where the result must be a null
> pointer.

Right (though I'm not sure what the phrase "by chance" adds to this;
most programmers don't flip a coin to decide whether to use a null
pointer constant or not).

>          So whatever definition the implementation gives, it must give
> a definition that converts 0 to a null pointer if the zero was a null
> pointer constant,

Not really.  The conversion of a CAE with value 0 to a pointer is *not*
implementation-defined.  Look at N1570 6.3.2.3.  Paragraph 3 says:

    An integer constant expression with the value 0, or such an
    expression cast to type void *, is called a *null pointer
    constant*.  If a null pointer constant is converted to a
    pointer type, the resulting pointer, called a *null pointer*,
    is guaranteed to compare unequal to a pointer to any object
    or function.

This defines the terms "null pointer constant" and "null pointer".
(It's an incomplete definition of "null pointer", since there are ways
to obtain null pointers without using a null pointer constant, but
that's beside the point.)

And paragraph 5 says:

    An integer may be converted to any pointer type. Except as
    previously specified, the result is implementation-defined,
    [...]

So it's only integer-to-pointer conversions *other than* those
already defined in paragraph 3 (null pointer constants) that
are implementation-defined.  An implementation's documentation
needn't say anything about the result of converting a constant
0 to a pointer type; it's already implied by stating that it's a
conforming implementation.

>                   therefore the definition must convert all zeroes to
> null pointers.

This turns out not to be correct.  It's certainly very common that
converting a non-constant integer expression with value 0 to a
pointer type yields a null pointer, but it's not required.

Consider a hypothetical implementation with the following
characteristics:

    All pointer types have the same representation.

    A null pointer is represented as all-bits-one (not as the more
    common all-bits-zero).

    Except as specified by the definition of "null pointer constant",
    conversion of an integer value to a pointer type simply copies
    or reinterprets the bits of the integer's representation.
    (If the integer is smaller than a pointer, it's zero-extended;
    if it's larger, high-order bits are ignored.)

    For purposes of the following demonstration, printf's "%p"
    format produces the representation of the pointer in hexadecimal.

In this implementation, the following program:

    #include <stdio.h>
    
    static void show(char *name, void *p) {
        printf("%-15s = %p is %sa null pointer\n",
               name, p, p == NULL ? "" : "NOT ");
    }
    
    int main(void) {
        unsigned int zero = 0;
        unsigned int all_ones = -1;
        show("(void*)0", (void*)0);
        show("(void*)zero", (void*)zero);
        show("(void*)all_ones", (void*)all_ones);
        return 0;
    }

would produce this output:

    (void*)0        = 0xffffffff is a null pointer
    (void*)zero     = 0x00000000 is NOT a null pointer
    (void*)all_ones = 0xffffffff is a null pointer

How would this implementation violate the C standard?

The point of controversy is whether an integer-to-pointer conversion
for a constant 0 is required to yield the same result as a conversion
for a non-constant 0.  I assert that it isn't.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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