Anand Hariharan wrote:
> On May 21, 1:14 am, Richard Heathfield <r...@[EMAIL PROTECTED]
> wrote:
>> Anand Hariharan said:
>>
>> > On Wed, 14 May 2008 18:32:05 +0000, Richard Heathfield wrote:
>>
>> <snip>
>>
>> >> usr_data is converted to &usr_data[0]
>>
>> > Is that true?
>>
>> Yes.
>>
>> > I thought usr_data[0] implies a dereference,
>>
>> You don't actually need to access the value of usr_data[0] (which is
>> what "dereference" means) just to establish its address, which is
>> what & does. (Indeed, without knowing its address, it's hard to see
>> how you *could* evaluate it.)
>>
>
> Going by that reasoning, one could say that the address of one past
> the last element of an array is
>
> &a[n]
>
> as opposed to
>
> a + n
>
> but I have believed that &a[n] involves a dereference and hence UB.
In C++ and C89 you are technically correct, although it is very unlikely
that '&a[n]' actually results in different behaviour than 'a+n'.
The reason is that a compiler has to produce perversely inefficient code
to let '&a[n]' do anything absurd. Even the most basic optimiser would
take the unused dereference out of the code.
In C99, it is even explicitly specified that '&a[n]' is the exact
equivalent of 'a+n'.
>
>
>> > hence
>> > &usr_data[0] is better expressed as &(*(usr_data)) rather than
>> > usr_data.
>>
>> I don't see why that's better. Surely a simple expression is better
>> than a complicated expression? We write n, not (n + 8) - 8. We write
>> n, not (n * 10) / 10. We write sqrt(n), not n/sqrt(n). For the more
>> complicated expression to be worth writing, it has to achieve
>> something useful. What does &(*(usr_data)) tell us that usr_data
>> doesn't?
>>
>
> Okay. I should have worded it as "is actually equivalent to" rather
> than "better expressed as". I don't believe &(*(usr_data)) are
> usr_data identical in that, if usr_data were to be a pointer that
> didn't point to anything or pointed to invalid memory, then the former
> is UB.
But that was not the situation where the claim of equivalence was made.
The claim that the expressions 'usr_data' and '&usr_data[0]' are
equivalent was made in the context that usr_data is declared as an
array. In that context, it is impossible for '&usr_data[0]', or
equivalently '&*usr_data' to result in UB (as an array must by
definition have at least one element).
>
> sincerely,
> - Anand
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/


|