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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wenjie

    &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

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


    "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
    news:vfgkgtlo7p bca@corp.supern ews.com...[color=blue]
    > "Wenjie" <gokkog@yahoo.c om> wrote...[color=green]
    > > 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?[/color]
    >
    > 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.[/color]

    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


    Comment

    • Victor Bazarov

      #3
      Re: &amp;a v.s. a v.s. &amp;a[0]

      "John Harrison" <john_andronicu s@hotmail.com> wrote...[color=blue]
      >
      > "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
      > news:vfgkgtlo7p bca@corp.supern ews.com...[color=green]
      > > "Wenjie" <gokkog@yahoo.c om> wrote...[color=darkred]
      > > > 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?[/color]
      > >
      > > 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.[/color]
      >
      > 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.[/color]

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

      Victor


      Comment

      Working...