Consider the following array:
This is an array of ten characters from the letters, ‘F’ to ‘O’. The name of this array is arr1. Consider the following array:
The name of this array is arr2. Notice that both contents, are the same. arr2 would be a deep copy of arr1 if both initializer_lists are in different regions in the computer’s memory. This article explains, manual deep copying of the array, and automatic deep copying of the array, in C++.
Article Content
– Manual Deep Copying of the Array
– Automatic Deep Copying of the Array
– Conclusion
Manual Deep Copying of the Array
With this approach, two arrays of the same size are created. The first one has content while the second one does not have content. The content of the first one is copied into the second one using the for-loop. The following program illustrates this:
using namespace std;
int main(){
#define size 10
char arr1[] = {'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'};
char arr2[size];
for (int i=0; i<size; i++)
arr2[i] = arr1[i];
return 0;
}
The first line of the program includes the C++ iostream header (library) for input and output. This first line is a directive. The second line is not a directive. It is a statement. It insists that any name not preceded by std:: is of the standard namespace. Thereafter is the C++ main function.
The first line in the main() function is a directive. It defines the size of both arrays, to be 10. It does not end with a semicolon. It ends with the press of the keyboard Enter Key ‘\n’ . This line could equally have been “int size = 10;”. The line after is a statement that defines the first array. The line following is the declaration of the second array, without practical initialization, but with the same size.
The next code segment in the main function, does the copy, element by element, from the first to the second array.
The following two code segments can be added, to print both array contents at the terminal (console):
cout << arr1[i] << ' ';
cout << endl;
for (int i=0; i<size; i++)
cout << arr2[i] << ' ';
cout << endl;
The output should be,
F G H I J K L M N O
Automatic Deep Copying of the Array
Here, the std::copy() function, of the C++ algorithm library is used. This means, the algorithm header (library) has to be included into the program. There is no need to copy, element by element, here. The prototype of the std::copy() function is:
constexpr OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result);
The first argument is an iterator that points to the first element of the source container (list). The second argument is an iterator that points just beyond the last element of the source container. The third argument is an iterator that points to the first element of the empty destination container, which should already have been declared.
This syntax can be interpreted for arrays with the following prototype:
constexpr OutputIterator copy(arr1, pointer-to-just-past-arr1, arr2);
pointer-to-just-past-arr1 is the same as, arr1 + size. So, the following program, does automatic deep copying of one array to another:
#include <algorithm>
int main() {
int size = 10;
char arr1[] = {'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'};
char arr2[size];
copy (arr1, arr1+size, arr2); //auto copying
return 0;
}
Note the inclusion of the algorithm library. “int size = 10;” has been used, instead of “char arr2[size];”. Note that the arrays still had to be of the same size but with the second one empty. The automatic copying statement is:
The function did not have to be preceded by “std::” , since there is “using namespace std;” at the top of the program.
The following two code segments can be added to print both array contents at the terminal (console):
cout << arr1[i] << ' ';
cout << endl;
for (int i=0; i<size; i++)
cout << arr2[i] << ' ';
cout << endl;
The output should be,
F G H I J K L M N O
Conclusion
In C++ an array can be copied manually (by hand) or automatically using the std::copy() function from the C++ algorithm library. In computer programming, there is shallow copying and there is deep copying. Shallow copying is when two different array names (old and new) refer to the same content in memory. Deep copying is when the two different array names refer to two independent, but same content, in memory. This article has dealt with deep copying and not shallow copying.
With manual deep copying approach, two arrays of the same size are created. The first one has content, while the second one does not have content. The content of the first one is copied to the second one, using the for-loop.
Automatic deep copying of one array to another in C++ involves the std::copy() function of the C++ algorithm library. This means, the algorithm header (library) has to be included into the program. There is no need to copy element by element with the for-loop in this case since copying is automatic. The prototype for the std::copy() function, interpreted for the array, is:
constexpr OutputIterator copy(arr1, pointer-to-last-element-of-arr1, arr2);