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


Groups > comp.lang.c > #16651

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 02:35 +0000
Organization A noiseless patient Spider
Message-ID <20120202173230.529@kylheku.com> (permalink)
References <jgfa2r$h8h$1@dont-email.me> <20120202163655.24@kylheku.com> <jgfcn5$t4p$1@dont-email.me>

Show all headers | View raw


On 2012-02-03, Shao Miller <sha0.miller@gmail.com> wrote:
> Thank you for your response.  I agree that they point to the same 
> location.  But 6.5.9p6 doesn't state that pointing to the same location 
> is sufficient to yield an equality.

It does if you interpret "same object" as being "same location".

What is an object? "A region of data storage in the execution environment,
the contents of which can represent values".

Pointers to the same object are pointers to the same region of data storage.

>>> 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.
>>
>
> Agreed.  But does the result create an issue for the subject of 
> "bounds-checking?"

I don't think there can be any expectation of bounds checking between two views
of the same storage through a union.  There is just no sense there of a pointer
from one straying out of bounds and into the other.

> If they are not distinct objects, then they are the same object, right? 
>   Well what type is the object?  Is it an 'int'?  If so, is it treated 
> as an 'int[1]' for the purposes of pointer arithmetic?  Or is it an 
> array object, instead?  If so, how many elements in the array?  Could 
> you do:

It is all these things simultaneously. You could ask these same questions
of a non-union object.  Given int x[5], what is x[1]? Is it just an int?
Or is it a portion of the whole array? So what type is it?

ISO C uses the term "subobject" for embedded objects. x[1] is an int object
which s a subobject of the int[5] array.

Both arrays involved in the union have contain subobjects of type int which
correspond together.

If two views of a different type are aliased through a union, then
the type is whichever one is the last through which a value is stored.
This is from the special rules about unions.

I think it should hold for this kind of array aliasing. (But you're not even
asking about if it's okay to store to one array and read the other; just
whether certain pointers are reliably equal.)

>    &test.a[1][1] - 2;

Also consider a[0][3].  This kind of "wrong geometry" access is in a kind of
gray area, where the standard isn't of a lot of help.  

Right or wrong, some C programs are going to do this and find it to be quite
portable.  So if you're doing bounds-checking, it's not entirely realistic to
insist on diagnose such things. A programmer who doesn't consider that to be a
bounds error will be irked by the diagnostics, perceived to be a nuisance.

On the other hand, the "wrong geometry" access to a[0][3] could be unintended,
and indicate a bug. Anotehr programmer might be thankful to have that diagnosed
(maybe even the same programmer, in a different programming situation).

There have been c.l.c discussions about this once in a while; I remember some
from many years ago. Some of the arguments hinged on whether such an access is
done blatantly with array indexing, or displacement of pointer directly obtaind
from "array" decay. Under that kind of hair-splitting &a[0][0] + 3 seems less
wrong than than a[0][3] because the former has a pointer to just an int, which
is then being displaced, in a way that is disconnected from the geometry of the
array.

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