Talk About Network

Google





Programming > C - C++ Learning > Re: Pointer to ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 16 of 24 Topic 4140 of 4400
Post > Topic >>

Re: Pointer to an array

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > May 21, 2008 at 07:39 PM

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/
 




 24 Posts in Topic:
[C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 15:14:02 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 16:30:48 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-14 17:44:04 
Re: [C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 18:15:40 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 18:32:05 
Re: [C] Pointer to an array
Aggro <spammerdream@[E  2008-05-14 18:36:49 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-14 18:55:56 
Re: [C] Pointer to an array
Keith Thompson <kst-u@  2008-05-14 13:10:50 
Re: [C] Pointer to an array
Andrey Tarasevich <and  2008-05-14 11:41:42 
Re: [C] Pointer to an array
Anand Hariharan <znvyg  2008-05-21 04:44:12 
Re: [C] Pointer to an array
"Alf P. Steinbach&qu  2008-05-21 06:38:33 
Re: [C] Pointer to an array
Richard Heathfield <rj  2008-05-21 06:14:08 
Re: [C] Pointer to an array
Barry Schwarz <schwarz  2008-05-25 10:44:26 
Re: Pointer to an array
Anand Hariharan <mailt  2008-05-21 07:50:59 
Re: Pointer to an array
"Alf P. Steinbach&qu  2008-05-21 17:11:16 
Re: Pointer to an array
Bart van Ingen Schenau &l  2008-05-21 19:39:27 
Re: Pointer to an array
Andrey Tarasevich <and  2008-05-21 11:50:21 
Re: Pointer to an array
Anand Hariharan <mailt  2008-05-22 13:51:12 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-26 16:57:06 
Re: [C] Pointer to an array
Francis Glassborow <fr  2008-05-26 19:39:07 
Re: [C] Pointer to an array
Keith Thompson <kst-u@  2008-05-26 14:14:19 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-27 04:01:45 
Re: [C] Pointer to an array
Ben Bacarisse <ben.use  2008-05-27 04:23:15 
Re: [C] Pointer to an array
Andrey Tarasevich <and  2008-05-14 11:32:35 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Fri Jan 9 6:45:05 PST 2009.