Copying array to an array, I really thought I knew what was going on.

Discussion in 'C++' started by DaTurk, Sep 10, 2007.

  1. DaTurk

    DaTurk Guest

    This is probably a very silly, and simple question. If I'm coding in
    CLI, and I want to copy an array to an array, not a deep copy, just
    something of the nature arr1 = arr2, what is going on?

    I assumed the address of the first element is getting copied, so i
    essentially have two handles to the same memory. And changes in one
    would be reflected in the other.

    But what if I have something like this

    public value struct MyStruct
    {
    Int32 MyNumber;
    };

    array<MyStruct>^ arr1 = gcnew array<MyStruct(10);

    //We fill the array with 10 MyStruct

    array<MyStruct>^ arr2 = arr1;

    Now what do we have here? since it's a value struct, did we
    essentially do a deep copy?
    DaTurk, Sep 10, 2007
    #1
    1. Advertising

  2. Re: Copying array to an array, I really thought I knew what was goingon.

    On 2007-09-10 21:11, DaTurk wrote:
    > This is probably a very silly, and simple question. If I'm coding in
    > CLI, and I want to copy an array to an array, not a deep copy, just
    > something of the nature arr1 = arr2, what is going on?


    If you are coding in C++/CLI you are asking in the wrong group. This
    group is for discussing C++ (and while C++/CLI might have a name and
    syntax similar to C++ they are two very different things). I would
    suggest you ask your question in either one of the microsoft.public.*
    groups, microsoft.public.dotnet.languages.vc is probably the one you
    want. Another good place to ask is forums.microsoft.com

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Sep 10, 2007
    #2
    1. Advertising

  3. DaTurk

    Jim Langston Guest

    "DaTurk" <> wrote in message
    news:...
    > This is probably a very silly, and simple question. If I'm coding in
    > CLI, and I want to copy an array to an array, not a deep copy, just
    > something of the nature arr1 = arr2, what is going on?
    >
    > I assumed the address of the first element is getting copied, so i
    > essentially have two handles to the same memory. And changes in one
    > would be reflected in the other.
    >
    > But what if I have something like this
    >
    > public value struct MyStruct
    > {
    > Int32 MyNumber;
    > };
    >
    > array<MyStruct>^ arr1 = gcnew array<MyStruct(10);
    >
    > //We fill the array with 10 MyStruct
    >
    > array<MyStruct>^ arr2 = arr1;
    >
    > Now what do we have here? since it's a value struct, did we
    > essentially do a deep copy?


    Ignoring hte fact that this isn't C++ code but something else (C# maybe?)
    and presuming ^ is some type of pointer...

    If this code was:

    public value struct MyStruct
    {
    Int32 MyNumber;
    };

    MyStruct* arr1 = new MyStruct[10];

    //We fill the array with 10 MyStruct

    MyStruct* arr2 = new MyStruct[10];

    You would want to iterate over the array copying elements. The manual
    method is:

    for ( size_t i = 0; i < 10; ++i )
    arr2 = arr1;

    This is a very common task, however, so TPTB added it to the stl with
    std::copy

    std::copy( arr1.begin(), arr1.end(), std::inserter(arr2, arr2.end()) );

    The syntax for the 3rd parameter of std::copy here may be wrong, but it
    should work.

    Other than that, ask in a newsgroup related to your language, whatever it
    is.
    Jim Langston, Sep 11, 2007
    #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.
Similar Threads
  1. =?Utf-8?B?cm9kY2hhcg==?=

    i knew this before...

    =?Utf-8?B?cm9kY2hhcg==?=, Nov 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    910
    =?Utf-8?B?cm9kY2hhcg==?=
    Nov 10, 2005
  2. Replies:
    13
    Views:
    541
    Chris Uppal
    Jan 31, 2005
  3. =?ISO-8859-1?Q?Morris_Carr=E9?=

    I knew there was an mystery to it !

    =?ISO-8859-1?Q?Morris_Carr=E9?=, May 9, 2004, in forum: Python
    Replies:
    0
    Views:
    319
    =?ISO-8859-1?Q?Morris_Carr=E9?=
    May 9, 2004
  4. asrock
    Replies:
    0
    Views:
    296
    asrock
    Feb 23, 2009
  5. Replies:
    1
    Views:
    104
    Mirco Wahab
    Jul 15, 2008
Loading...

Share This Page