In C/C++, we can assign a struct (or class in C++ only) variable to another variable of same type. When we assign a struct variable to another, all members of the variable are copied to the other struct variable. But what happens when the structure contains pointer to dynamically allocated memory and what if it contains an array?
In the following C++ program, struct variable st1 contains pointer to dynamically allocated memory. When we assign st1 to st2, str pointer of st2 also start pointing to same memory location. This kind of copying is called Shallow Copy.
# include <iostream> # include <string.h> using namespace std; struct test { char *str; }; int main() { struct test st1, st2; st1.str = new char[20]; strcpy(st1.str, "GeeksforGeeks"); st2 = st1; st1.str[0] = 'X'; st1.str[1] = 'Y'; /* Since copy was shallow, both strings are same */ cout << "st1's str = " << st1.str << endl; cout << "st2's str = " << st2.str << endl; return 0; } |
Output:
st1’s str = XYeksforGeeks
st2’s str = XYeksforGeeks
Now, what about arrays? The point to note is that the array members are not shallow copied, compiler automatically performs Deep Copy for array members.. In the following program, struct test contains array member str[]. When we assign st1 to st2, st2 has a new copy of the array. So st2 is not changed when we change str[] of st1.
# include <iostream> # include <string.h> using namespace std; struct test { char str[20]; }; int main() { struct test st1, st2; strcpy(st1.str, "GeeksforGeeks"); st2 = st1; st1.str[0] = 'X'; st1.str[1] = 'Y'; /* Since copy was Deep, both arrays are different */ cout << "st1's str = " << st1.str << endl; cout << "st2's str = " << st2.str << endl; return 0; } |
Output:
st1’s str = XYeksforGeeks
st2’s str = GeeksforGeeks
Therefore, for C++ classes, we don’t need to write our own copy constructor and assignment operator for array members as the default behavior is Deep copy for arrays.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Flexible Array Members in a structure in C
- Different ways to Initialize all members of an array to the same value in C
- Initialization of data members
- Static data members in C++
- Can we access private data members of a class without using a member or a friend function?
- What’s difference between “array” and “&array” for “int array[5]” ?
- Pointer to an Array | Array Pointer
- Difference between pointer to an array and array of pointers
- Jagged Array or Array of Arrays in C with Examples
- Array of Structures vs. Array within a Structure in C/C++
- Why C treats array parameters as pointers?
- Pointer vs Array in C
- Do not use sizeof for array parameters
- What is the difference between single quoted and double quoted declaration of char array?
- Declare a C/C++ function returning pointer to array of integer pointers
- How to pass a 2D array as a parameter in C?
- How to dynamically allocate a 2D array in C?
- What is Array Decay in C++? How can it be prevented?
- How to print size of array parameter in C++?
- How to create a dynamic 2D array inside a class in C++ ?

