Copy an Array

Copying an array in C++ can be done in various ways, ranging from manual element-by-element copy to using Standard Library functions. Below are some methods to copy an array to another in C++.

Copying an array in C++ involves creating a new array and then transferring each element from the original (source) array to the new (destination) array.

  1. Element-by-Element Copy: The most straightforward method is to loop through each element in the source array and copy its value to the corresponding position in the destination array. This is done by iterating from the first to the last index of the source array, and at each iteration, the value at the current index of the source array is copied to the same index in the destination array.

  2. Using Standard Library Functions: C++ offers built-in functions like std::copy that automatically handle the copying for you. When you use std::copy, you specify the beginning and end of the source array, as well as the beginning of the destination array, and the function takes care of the rest.

1. Element-by-Element Copy (Looping)

You can use a loop to copy each element from the source array to the destination array. Here's a basic example:

#include <iostream> using namespace std; int main() { int srcArray[] = {1, 2, 3, 4, 5}; int destArray[5]; for(int i = 0; i < 5; ++i) { destArray[i] = srcArray[i]; } // Print the destination array to verify for(int i = 0; i < 5; ++i) { cout << destArray[i] << " "; } return 0; }

2. Using copy from <algorithm>

The Standard Library provides the std::copy function that can copy elements from one array to another.

#include <iostream> #include <algorithm> using namespace std; int main() { int srcArray[] = {1, 2, 3, 4, 5}; int destArray[5]; copy(begin(srcArray), end(srcArray), begin(destArray)); // Print the destination array to verify for(int i = 0; i < 5; ++i) { cout << destArray[i] << " "; } return 0; }

 

Copying an array in these ways is necessary because arrays in C++ don't have built-in methods for operations like copying. Arrays in C++ are essentially pointers to the first element in a block of contiguous memory locations. When you assign one array to another using a simple assignment statement like destArray = sourceArray;, it will not perform a deep copy. Instead, it will make both arrays point to the same memory location, which is generally not the behavior you want when you say "copy an array."

Here's what happens if you try to directly assign one array to another:

int sourceArray[] = {1, 2, 3, 4, 5}; int *destArray = sourceArray; // This won't copy the array, it makes destArray point to sourceArray

Now, destArray and sourceArray point to the same block of memory, and changes to the array via one pointer will affect the "copy," which is not a real copy at all. That's why you have to use to the manual methods I described earlier for copying arrays in C++:

For more complex types, or for collections that might change dynamically, people often use containers from the C++ Standard Library like vector, which do have built-in methods for copying. But for basic arrays, you have to manage the copying yourself.

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark