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


Groups > comp.lang.c > #16646

Re: Pointer Equality for Different Array Objects

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: Pointer Equality for Different Array Objects
Date 2012-02-03 00:47 +0000
Organization A noiseless patient Spider
Message-ID <20120202163655.24@kylheku.com> (permalink)
References <jgfa2r$h8h$1@dont-email.me>

Show all headers | View raw


On 2012-02-03, Shao Miller <sha0.miller@gmail.com> wrote:
> (More bounds-checking.)
>
> Wait a minute...  N1256 6.5.9p6:
>
>    "Two pointers compare equal if and only if both are null pointers, 
> both are pointers to the same object (including a pointer to an object 
> and a subobject at its beginning) or function, both are pointers to one 
> past the last element of the same array object, or one is a pointer to 
> one past the end of one array object and the other is a pointer to the 
> start of a different array object that happens to immediately follow the 
> first array object in the address space.94)"

This can only happen in a correct program if the two array-like objects
are themselves members of a larger array.

>
> So if we have:
>
>    union u_test {
>        int a[2][2];
>        int b[4];
>      };
>    union u_test test = { { { 0 } } };
>    int x = &test.a[1][1] == &test.b[3];
>
> Is 'x' zero or one?

One. These are the same address because the elements of an array are
contiguous. A simulated two dimensional array has the storage layout
of a one-dimensional array, and so a[1][1] corresponds to b[3].

> If we can claim, for the purposes of supporting bounds-checking, that 

For the purposes of bounds checking, you have two pointers here,
neither of which is out of bounds of the object from which they are derived.

A pointer comaprison (for exact equality, at that) does not create
a bounds-checking issue.

> '&test.a[1][1]' points into an 'int[2]' and nothing more, and that 
> '&test.b[3]' points into an 'int[4]' and nothing more, then aren't these 
> pointers pointing to distinct objects?

They are not distinct objects because they are overlapped in a union.

Even these have to compare equal:

  union { int x; double y; } u;

  &u.y == (double *) &u.x;

The members of a union all start at the same address. They not
distinct objects.

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


Thread

Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-02 19:34 -0500
  Re: Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-02 19:40 -0500
  Re: Pointer Equality for Different Array Objects Kaz Kylheku <kaz@kylheku.com> - 2012-02-03 00:47 +0000
    Re: Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-02 20:19 -0500
      Re: Pointer Equality for Different Array Objects pete <pfiland@mindspring.com> - 2012-02-02 20:45 -0500
        Re: Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-02 21:15 -0500
        Re: Pointer Equality for Different Array Objects Kaz Kylheku <kaz@kylheku.com> - 2012-02-03 02:36 +0000
      Re: Pointer Equality for Different Array Objects Kaz Kylheku <kaz@kylheku.com> - 2012-02-03 02:35 +0000
        Re: Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-03 01:27 -0500
          Re: Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-03 01:29 -0500
          Re: Pointer Equality for Different Array Objects Kaz Kylheku <kaz@kylheku.com> - 2012-02-03 07:11 +0000
            Re: Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-03 19:31 -0500
  Re: Pointer Equality for Different Array Objects Shao Miller <sha0.miller@gmail.com> - 2012-02-06 02:25 -0500

csiph-web