1. The source of deep and shallow copy of structure in C language
If a structure contains pointer variables, dynamic memory is allocated during the use of the structure, and mutual assignments are made between variables of the same type of structure, shallow copy and deep copy problems will be caused.
2. Shallow copy problem
Now let's consider the following code. First, we define a structure Student and declare two structure variables std1 and std2 in the main function. Then, we open up a 10 byte space in the heap area for the name of std1, copy the first address of the space to name, and copy a string to the space area pointed to by name through the strcpy function; Assign a value of 20 to the member variable age of std1. Finally, we use std1 to assign a value directly to std2. We draw the memory model corresponding to the program.
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Student { char *name; int age; }Student; int main(int argc, char *argv[]) { Student std1; Student std2; std1.name = (char *)malloc(10); std1.age = 20; strcpy(std1.name, "lele"); printf("std1--->name: %s, age: %d\n", std1.name, std1.age); //Assign std1 directly to std2 and print the value of std2 std2 = std1; printf("std2--->name: %s, age: %d\n", std2.name, std2.age); //Free the memory space pointed to by std1 member name free(std1.name); //Space can be released successfully //Free the memory space pointed to by std2 member name free(std2.name); //Since the space pointed to has been released, it cannot be released again return 0; }
From the memory model of the program, we can see. The member name of std1 and std2 points to the same memory area. Suppose we finish using std1 and std2 variables, the memory space pointed to by name will be released. First, we release the memory space pointed to by sdt1.name. At this time, the program will not have any problems and can be released normally. Then, we are freeing the memory space pointed to by std2.name, and the program will make an error. Due to, the memory space pointed to by std2.name has been released.
3. Deep copy problem
In order to avoid the above problems, for the structure containing pointer variables and the pointer variable members in the structure and pointing to a dynamically allocated memory space, we can use deep copy in the process of copying each other. Let's look at the following code.
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Student { char *name; int age; }Student; int main(int argc, char *argv[]) { Student std1; Student std2; std1.name = (char *)malloc(10); std1.age = 20; strcpy(std1.name, "lele"); printf("std1--->name: %s, age: %d\n", std1.name, std1.age); //Assign std1 directly to std2 and print the value of std2 std2 = std1; std2.name = (char *)malloc(10); //Reallocate a space for the name member strcpy(std2.name, std1.name); //Assign the string pointed to by std1.name member to the space pointed to by std.name printf("std2--->name: %s, age: %d\n", std2.name, std2.age); //Free the memory space pointed to by std1 member name free(std1.name); //Space can be released successfully //Free the memory space pointed to by std2 member name free(std2.name); //Since it does not point to the same space, it can be released successfully return 0; }
After copying std1 to std2, the member name of std2 points to a new space again, and assigns the content of the space pointed to by std1.name to the space pointed to by std2.name.
4. Summary
For structures that do not contain pointer variables and structures that contain pointer variables but do not point to a dynamically allocated memory space, the problem of deep and shallow copy will not be caused in the process of mutual assignment. For the above situations that can cause deep and shallow copy problems, the so-called deep and shallow copy is whether the assigned variable is reallocated after assignment, and the content is copied to the newly opened space. Deep and shallow copy problems are rare in C language, but they are common in C + +. Operator overloading is usually used to avoid these problems.
Note: due to the author's limited ability, you are welcome to point out anything wrong.