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

Discussion in 'C++' started by Wenjie, Jun 24, 2003.

  1. Wenjie

    Wenjie Guest

    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
    Wenjie, Jun 24, 2003
    #1
    1. Advertising

  2. "Victor Bazarov" <> wrote in message
    news:...
    > "Wenjie" <> 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
    John Harrison, Jun 24, 2003
    #2
    1. Advertising

  3. "John Harrison" <> wrote...
    >
    > "Victor Bazarov" <> wrote in message
    > news:...
    > > "Wenjie" <> 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
    Victor Bazarov, Jun 24, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page