Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   &a v.s. a v.s. &a[0] (http://www.velocityreviews.com/forums/t267653-and-a-v-s-a-v-s-and-a-0-a.html)

Wenjie 06-24-2003 12:16 PM

&a v.s. a v.s. &a[0]
 
Hello,


I have an array: unsigned char a[1024];
I want to copy some data into it:
memcpy(a, anotherArray, sizeof(a));

But I saw somebody is using this:
memcpy(&a, anotherArray, sizeof(a));
And it passed the tests (VC++6.0).

Could someone confirm me that the 2nd use of
of &a is not valid? Why?
Also I would think it is OK to do:
memcpy(&a[0], anotherArray, sizeof(a));


Best regards,
Wenjie

John Harrison 06-24-2003 04:39 PM

Re: &a v.s. a v.s. &a[0]
 

"Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message
news:vfgkgtlo7pbca@corp.supernews.com...
> "Wenjie" <gokkog@yahoo.com> wrote...
> > I have an array: unsigned char a[1024];
> > I want to copy some data into it:
> > memcpy(a, anotherArray, sizeof(a));
> >
> > But I saw somebody is using this:
> > memcpy(&a, anotherArray, sizeof(a));
> > And it passed the tests (VC++6.0).
> >
> > Could someone confirm me that the 2nd use of
> > of &a is not valid? Why?

>
> I don't think someone would be able to confirm that because
> it's valid. Expressions 'a' and '&a' have different _type_
> but the same _value_. Used with memcpy, they are converted
> to 'void*' of the same value, which then is passed to memcpy
> and gives the same result.


You would pretty quickly see the difference though if you did this

memcpy(&a + 1, anotherArray, sizeof a - 1);

or this

memcpy(a + 1, anotherArray, sizeof a - 1);

or this

memcpy(&a[1], anotherArray, sizeof a - 1);

The first is a bug, the second and third are OK. Try printing out the
pointer values to see the difference.

john



Victor Bazarov 06-24-2003 04:50 PM

Re: &a v.s. a v.s. &a[0]
 
"John Harrison" <john_andronicus@hotmail.com> wrote...
>
> "Victor Bazarov" <v.Abazarov@attAbi.com> wrote in message
> news:vfgkgtlo7pbca@corp.supernews.com...
> > "Wenjie" <gokkog@yahoo.com> wrote...
> > > I have an array: unsigned char a[1024];
> > > I want to copy some data into it:
> > > memcpy(a, anotherArray, sizeof(a));
> > >
> > > But I saw somebody is using this:
> > > memcpy(&a, anotherArray, sizeof(a));
> > > And it passed the tests (VC++6.0).
> > >
> > > Could someone confirm me that the 2nd use of
> > > of &a is not valid? Why?

> >
> > I don't think someone would be able to confirm that because
> > it's valid. Expressions 'a' and '&a' have different _type_
> > but the same _value_. Used with memcpy, they are converted
> > to 'void*' of the same value, which then is passed to memcpy
> > and gives the same result.

>
> You would pretty quickly see the difference though if you did this
>
> memcpy(&a + 1, anotherArray, sizeof a - 1);
>
> or this
>
> memcpy(a + 1, anotherArray, sizeof a - 1);
>
> or this
>
> memcpy(&a[1], anotherArray, sizeof a - 1);
>
> The first is a bug, the second and third are OK. Try printing out the
> pointer values to see the difference.


The point is that they will still compile. The "bug" is due
to undefined behaviour, not due to "illegal C++ construct".

Victor




All times are GMT. The time now is 06:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.