I am a software engineer. I have been come across C++,MFC, .net technologies. I like playing video games & reading books.
When we pass base data types (int, float etc.,) to a function a copy from the calling piece of code to called function occurs. Now look at the below piece of code which does a simple function call:
The copy I am taking occurs between x=>loc_X and y=>loc_Y. The content of variable x in main function scope is copied to the variable loc_X, which is in the AddNumbers function scope. This holds true for the next parameter loc_Y also. This copying is shown below:
OK. This is good for standard data types. A class can have one or more data members. How the copy occurs between the data members is what we are going to deal with this hub. When the Hub progresses, I will explain Shallow Copy, Deep Copy and the need for our own copy constructor.
2. ShalloC class
To demonstrate the need for the copy constructor, we will first define an example class. This example class is ShalloC. This class contains only one integer pointer as private data member as shown below:
The constructor will create a memory location in a heap and copy the passed in value m to the heap content. This code is shown below:
The Get and Set functions are used to get the heap memory content value and Set the heap memory content respectively. Below is the code that sets and gets the integer heap memory value:
Finally, there is a function to print the heap content value in the console window. The function is shown below:
Now you may get the idea of what the ShalloC class will do. At present it has a constructor that creates a heap memory and in the destructor we clear the memory created as shown in the below code:
3. Shallow Copy vs. Deep Copy
In the Program main we created two Objects ob1 and ob2. The object ob2 is created using the copy constructor. How? And where is the "copy constructor".? If you look at the statement ShalloC ob2 = ob1 ; you clearly know that the ob2 is not yet created and in the mean time ob1 is already created. Hence, a copy constructor is invoked. Even though the copy constructor not implemented, the compiler will provide default copy constructor. Once both the objects are created we print the values in ob1 and ob2.
After printing the values in ob1 and ob2 we change the value of the object ob1’s data member pointed value to 12. Then both the values of ob1 and ob2 are printed. The code and its output are shown below:
The output shows value 12 for both ob1 and ob2. Surprisingly, we modified the data member of the object ob1 only. Then, Why the changes are reflected on both the objects? This is what called shallow copy induced by the compiler provided default constructor. To understand this look at the below picture:
When object ob1 is created, the memory to store an integer is allocated in the heap. Let us assume the heap memory location address is 0x100B. This address is what stored in the x. Remember x is an integer pointer. The value stored in the pointer variable x is the address 0x100B and the content of the address 0x100B is value 10. In the example, we want to deal with the content of the address 0x100B we use the pointer de-referencing like *x. The compiler provided copy constructor copies the address stored in the ob1(x) to ob2 (x). After the copy, both pointers in ob1 and ob2 points to the same object. So changing the 0x100B through ob1.SetX(12) is reflected back in the ob2. Now you got how the result is printing 12 for both the objects ob1 and ob2.
How do we avoid the above-shown problem? We should perform the deep copy by implementing our own copy constructor. So a user defined copy constructor is required to avoid the problem of shallow copy. Below is the copy constructor:
Once we inject this copy constructor to the ShalloC class, the x pointer in the object ob2 will not point to the same heap location 0x100B. The statement x = new int; will create the new heap location and then copies the value of obj content to new heap location. The output of the program, after introducing our own copy constructor is shown below:
The entire code is shown below:
sirama (author) on September 25, 2019:
Without pointer, it is stack. Means separate copies holding the values on its own. This is applicable to heaps. Avoid, two stack location holding the same address of a heap
Aniket katade on September 25, 2019:
Without a pointer variable also it works fine?? what is the reason
Ibrahim on July 06, 2019:
Gumma, Manjunatha on July 01, 2019:
Very good explanation.
rajasekhar on May 18, 2019:
Thank you so much for better explanation with better example
Umair Sajid on March 17, 2019:
It so helped me thank you.
Ali Ahmad on October 14, 2018:
it was helpful
rakshith on September 27, 2018:
excellent. Thank you ! crisp and clear.
Arjun Yadav from India on July 09, 2018:
thanks, it was helpful!!!!!!
swarna on June 19, 2018:
very nice post. provided a deep understanding of deep copy and shalloe copy
Ramy Tanios on June 12, 2018:
SajjadNazari on June 02, 2018:
very clear and easy to undrestand
CAD on May 17, 2018:
dshri on May 15, 2018:
the session is very helpful.
DHiraj on June 02, 2017:
Nicest shallow copy and deep copy tutorial so far i found on internet. Just a case that when using with gcc on my redhat box, it gave double free of memory error while running then i worked it around by commenting the destructor and it worked fine. I dont know the reason, can you suggest possible flaw?