-3

How would I copy an array to a new array if i want the new array to not be dependent on the original array in C++. I know it has to do with using pointers but i am new to C++ and don't quite understand how this works

Sammy Berry
  • 23
  • 1
  • 4

1 Answers1

4

Short Answer

If you have 2 arrays defined as 10-byte each, say unsigned char arrayOne[10], arrayTwo[10];, they are already independent, and copying one into the other could be easily done this way:

for(unsigned i=0; i < 10; i++) 
     arrayTwo[i] = arrayOne[i];

you could also use memcpy:

memcpy(arrayTwo, arrayOne, 10); // 10 bytes copied from arrayOne to arrayTwo

and you could also use std::copy:

std::copy(arrayOne, arrayOne+10, arrayTwo); // Copy 10 elements from arrayOne to ArrayTwo

Long Answer

If you perform a copy of an array, the new array won't have any dependency on the original after the copy, if I am correctly understanding what you mean with "dependency".

It is more clear if you think of it from the memory point of view, and not from language point of view. An array is a sequence of values stored together in memory. Lets consider this code:

unsigned char myArray[10] { 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };

After compilation and execution, these 10 bytes are stored together at a given location in memory. Imagine that location to be 0x1234 (For simplicity, consider our computer to have a 16-bits memory):

Location |0x1234|0x1235|0x1236| ....
         ---------------------------------------------------
Content  |  12  |  14  |  16  | .....
         ---------------------------------------------------

In this context, you have 10 bytes stored, and a 2-byte variable called myArray which is also taking 2 bytes in memory, like this:

            myArray             myArray values
         ---------------      ----------------------
Location |0x0333|0x0334| .... |0x1234|0x1235|0x1236| .....
         ---------------------------------------------------
Content  | 0x12 | 0x34 | .... |  12  |  14  |  16  | .....
         ---------------------------------------------------

Then, when you do something like this:

myArray[2] = 99;

You are effectively doing this:

*(myArray + 2) = 99;

which, in turn, means this:

*(0x1234 + 2) = 99;

This is the way pointers work. myArray is a pointer: a variable that holds the address where your array starts. Knowing that the array has all the elements consecutive, and each element occupies 1 byte, accessing the element 8 is accessing the start of the array plus 8, that is *(myArray + 8) or myArray[8].

So, if you wanted to make an independent copy of the array, you need another 10 bytes located in a different place in memory, and another pointer to the start of these new 10 bytes: that is, another array. This code does it:

unsigned char myArray[10] { 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 };
unsigned char otherArray[10];

for(unsigned i=0; i < 10; i++) 
     otherArray[i] = myArray[i];

This creates two independent arrays with the same content: two spaces of 10 bytes, with their two pointers to their locations in memory. Let's do an opperation to the second array:

otherArray[2] = 99;

imagining that otherArray = 0x4455, this would be the same as:

*(0x4455 + 2) = 99;

Let's imagine the result of this in our imaginary 16-bits memory:

            myArray             myArray values
         ---------------      ----------------------
Location |0x0333|0x0334| .... |0x1234|0x1235|0x1236| .....
         ---------------------------------------------------
Content  | 0x12 | 0x34 | .... |  12  |  14  |  16  | .....
         ---------------------------------------------------
                                             ^^^^^^
                                             Original left untouched

            otherArray          otherArray values
         ---------------      ----------------------
Location |0x0422|0x0423| .... |0x4455|0x4456|0x4457| .....
         ---------------------------------------------------
Content  | 0x44 | 0x55 | .... |  12  |  14  |  99  | .....
         ---------------------------------------------------
                                              ^^^^^
                                            Changed value

Then, we have our 2 arrays, one of them being copy of the other, but completely independent.

ronaldo
  • 455
  • 3
  • 9
  • I wouldn't expect the `memcpy` version to be any faster than the loop; that comment might suggest to newbies that there is some advantage to using `memcpy` when in fact that function should be avoided. Because it's easy to get the parameters wrong (as you in fact did!) The loop or `std::copy` would be preferable. – M.M May 30 '15 at 12:56
  • I agree with you in that the comment may be not appropriate for a newbie and can lead to confusion. I'll modify it. However, are you sure that parameters are wrong for `memcpy`? http://www.cplusplus.com/reference/cstring/memcpy/ – ronaldo May 30 '15 at 13:01