Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Pointer Equality for Different Array Objects |
| Date | 2012-02-06 02:25 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jgnv86$55h$1@dont-email.me> (permalink) |
| References | <jgfa2r$h8h$1@dont-email.me> |
On 2/2/2012 19:34, Shao Miller 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)"
>
> 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?
>
> If we can claim, for the purposes of supporting bounds-checking, that
> '&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?
>
> If they're pointing to the same object, why might they not be for the
> purposes of 6.5.6p8?
>
> Just to illustrate an identical [declaration]:
>
> int x = (*(test.a + 1) + 1) == (test.b + 3);
>
>
Maybe using a 'typedef' makes it look different?
typedef int array_of_two_ints[2];
union u_test {
array_of_two_ints a[2];
int b[4];
};
union u_test test = { { { 0 } } };
int x;
array_of_two_ints * ptr_into_array_of_two_ints;
int * ptr_into_int;
/*
* Point into the second element of 'a', so the
* element type is 'array_of_two_ints'
*/
ptr_into_array_of_two_ints = &test.a[1];
/*
* Point into the second element of the array object
* with type 'array_of_two_ints', so the element
* type is 'int'
*/
ptr_into_int = &ptr_into_array_of_two_ints[1];
/*
* 'ptr_into_int' does _not_ point to:
* - A subobject at the beginning of the 'test.b'
* array object
* - The array object 'test.b'
* - A function
* - No object (it's non-null)
* - One past the array object 'test.b'
* - An array object that happens to immediately
* follow 'test.b' in the address space
* - An object that is not an element of an array
* object
* '&test.b[3] points to:
* - The fourth element of the 'test.b' array
* object
*/
x = ptr_into_int == &test.b[3];
x = ptr_into_int - &test.b[3];
Perhaps similarly:
struct s_common {
double d;
int i[2];
};
struct s_foo {
struct s_common common;
short s;
};
struct s_bar {
struct s_common common;
long l;
};
union u_foobar {
struct s_foo foo;
struct s_bar bar;
};
union u_foobar test;
int * p;
int * q;
int x;
p = &test.foo.common.i[1];
q = &test.bar.common.i[1];
/*
* 'p' does _not_ point to:
* - A subobject at the beginning of the
* 'test.bar.common.i' array object
* - The array object 'test.bar.common.i'
* - A function
* - No object (it's non-null)
* - One past the array object 'test.bar.common.i'
* - An array object that happens to immediately
* follow 'test.bar.common.i' in the
* address space
* - An object that is not an element of an array
* object
* 'q' points to:
* - The second element of the 'test.bar.common.i'
* array object
*/
x = p == q;
x = p - q;
Fortunately, of course, they point to the same byte in the same
contiguous region of data storage.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll 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