Szabolcs Borsanyi <> writes:
> On May 19, 4:59 am, David Thompson <dave.thomps...@verizon.net> wrote:
<snip>
>> It is guaranteed there is no hole between array elements, at any
>> level; thus a[0][0] is followed immediately by a[0][1], and a[0] as a
>> whole is followed immediately by a[1] as a whole, therefore a[0][9] is
>> followed immediately by a[1][0].
>
> Thanks, that's an important information. In covers that same statement
> that is actually being discussed in an other thread subjected as
> "Address of an array = address of its 1st".
I think this is not quite the same thing. There two things involved:
one is that there are no holes, the other is that a pointer to the
whole object will compare equal (when suitably converted) to a pointer
to an initial sub-object. There are specific and separate guarantees
about both.
> So I am happy to learn that there may no be an array overhead (data
> that belongs to the array and stored in the array before the first
> element)
>
>> And so it is guaranteed &a[0][10] == &a[1][0]. And, for example,
>> memcpy (tempi, (char*)a + 12*sizeof(int), sizeof(int))
>> will get you the value of a[1][2], and the reverse will set it.
>
> Good.
>
>> According to the strict abstract semantics, you can't dereference
>> &a[0][10] and can't form &a[0][11] or higher at all.
>
> I think this statement of yours is in contradiction with the previous
> one.
No, there is no contradiction. The standard is worded to allow
a pointer (at least in some cases) to know its bounds, i.e., the
extent of the thing it points to. The conversion to, and access via,
a char * is specifically guaranteed. De-referencing the pointer
&a[0][10] and constructing &a[0][11] are not although, of course, the
behaviour is undefined so it is perfectly OK for it to work (as it
does on most implementations).
> &a[0][10] is equivalent to a[0]+10. For those who forgot: a[0] is
> an array of ten integers. That is why you say, *&a[0][10], or a[0][10]
> is not correct. But in fact, here this array (a[0]) decays to a
> pointer,
> so &a[0][10] is &a[0][0]+10. From this you claimed that it compares
> equal to &a[1][0], and if two pointers compare equal and are of the
> same type, and one of them points to an object within its lifetime,
> then
> the other pointer points to the same object, and hence, it can also be
> dereferenced.
That is not guaranteed. &a[0][10] can be constructed because it is a
"one past the end" pointer, but you can't de-reference it. Well, you
probably can, but it is UB to do so. You have to image two pointers
that compare equal because they discard the bounds information at that
point, but one can't be de-referenced because the bounds kick in.
--
Ben.
|