Thread: Copying arrays...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Copying arrays...

    Hi,

    An exercise in my book asks me to define a second array initialised as a copy from the first array. I thought that you couldn't copy arrays?

    Would it simply be a case of looping through and initialising each element?

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Depends on the definition of "copy". You can copy each element and make a second array with separate memory, or you can copy the pointer and have two arrays that point to the same spaces in memory.

    If the first, then you stated one way of copying an array. If the second...

    int* array2 = array1; // copied array1

    This isn't really much of an exercise as it is a lesson in memory/array management.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks. So if I wanted 2 copies of the array I would use my first example, however if I wanted the same array to be copied, I would simply use a pointer?

    Thanks for the help.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    The difference lies in how you are managing the memory. The way I showed copies a pointer, which means you are accessing the exact same elements as the other array. Edit something in one array, and it will also change in the other.

    The way you described makes a second copy of the memory, so each array has its own. If I edit something in one array, it will not be seen in the other.

    Look at these examples.

    Code:
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int* a1 = new int[100]; // 100 integers
        int* a2 = a1;
    
        a1[0] = 5;
        cout<< a1[0] << " " << a2[0] << endl;
        a2[0] = 10;
        cout<< a1[0] << " " << a2[0] << endl;
    
        int a3[100] = {1}; // 100 integers, all set to 1
        int a4[100]; // 100 integers, different memory
    
        for(int i = 0; i < 100; i++) {
            a4[i] = a3[i]; // copy
        }
    
        cout<< a3[0] << " " << a4[0] << endl;
        a4[0] = 10;
        cout<< a3[0] << " " << a4[0] << endl;
    
        return 0;
    }

  5. #5
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Quote Originally Posted by scwizzo View Post
    Code:
        int a3[100] = {1}; // 100 integers, all set to 1
    Since the OP is currently in his 'soak it up like a sponge' phase (judging from previous posts) I'll just point out that this only sets a3[0] to 1, a3[1]-a3[99] will still be 0.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by adeyblue View Post
    Since the OP is currently in his 'soak it up like a sponge' phase (judging from previous posts) I'll just point out that this only sets a3[0] to 1, a3[1]-a3[99] will still be 0.
    Thanks. This is due to all others being set to the default value of the data type if I recall correctly?

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by scwizzo View Post
    The difference lies in how you are managing the memory. The way I showed copies a pointer, which means you are accessing the exact same elements as the other array. Edit something in one array, and it will also change in the other.

    The way you described makes a second copy of the memory, so each array has its own. If I edit something in one array, it will not be seen in the other.

    Look at these examples.

    Code:
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int* a1 = new int[100]; // 100 integers
        int* a2 = a1;
    
        a1[0] = 5;
        cout<< a1[0] << " " << a2[0] << endl;
        a2[0] = 10;
        cout<< a1[0] << " " << a2[0] << endl;
    
        int a3[100] = {1}; // 100 integers, all set to 1
        int a4[100]; // 100 integers, different memory
    
        for(int i = 0; i < 100; i++) {
            a4[i] = a3[i]; // copy
        }
    
        cout<< a3[0] << " " << a4[0] << endl;
        a4[0] = 10;
        cout<< a3[0] << " " << a4[0] << endl;
    
        return 0;
    }
    Thanks, nice and clear now.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by scwizzo View Post
    The difference lies in how you are managing the memory. The way I showed copies a pointer, which means you are accessing the exact same elements as the other array. Edit something in one array, and it will also change in the other.

    The way you described makes a second copy of the memory, so each array has its own. If I edit something in one array, it will not be seen in the other.

    Look at these examples.

    Code:
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int* a1 = new int[100]; // 100 integers
        int* a2 = a1;
    
        a1[0] = 5;
        cout<< a1[0] << " " << a2[0] << endl;
        a2[0] = 10;
        cout<< a1[0] << " " << a2[0] << endl;
    
        int a3[100] = {1}; // 100 integers, all set to 1
        int a4[100]; // 100 integers, different memory
    
        for(int i = 0; i < 100; i++) {
            a4[i] = a3[i]; // copy
        }
    
        cout<< a3[0] << " " << a4[0] << endl;
        a4[0] = 10;
        cout<< a3[0] << " " << a4[0] << endl;
    
        return 0;
    }
    Please use delete when using new! Or better yet, use smart pointers!
    You can also use std::copy instead of a tedious manual loop. It's good to know what the standard library offers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Is this poor coding? copying arrays of doubles
    By Hansie in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 02:34 PM
  3. copying strings to heap character arrays
    By SkyRaign in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2006, 02:08 PM
  4. Copying array's
    By big146 in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 01:25 PM
  5. copying character arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-20-2002, 05:39 PM