Shallow VS Deep Copy - explaining different copy mechanisms

adib      11/22/2020

Definitions of both terms

First lets give a simple declaration of these terms. Assume you have an object of one of containers like vectors. Now if you want to create another objects exactly like the first one, in most cases when we need an extra copy of an object, there is a need for the main object in order to do some modification on it(maybe).

But there is also exceptions that when you've created the copy object, you can forget about main object.
Stating 3 solutions for this problem is good enough for having an exact knowledge about different copying mechanisms.

1. Going through source object and copy each element

Use an iterator to go through each and every element of that container and using a for loop to copy all of them.

// Source object to be copied                            
vector<int> v1;

// push some value on v1
v1.push_back(43);
v1.push_back(32);
v1.push_back(21);

// the object we want to be made from v1
vector<int> v2;

// go through each and every element of v1 and push it on v2
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
    v2.push_back(*it);
}

// v2 is now copied from v1
for (vector<int>iterator it2 = v2.begin(); it2 != v2.end(); it2++)
{
    cout << *it2 << endl;
}
// result: 43
32
21

2. Using Copy Constructor

Use copy constructor of that container and achieve full copying of your object.

// main vector object
vector<int> v1;

// push some value on v1
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);

// copy v1 to v2 using copy constructor
vector<int> v2(v1);

// see if its copied
for (vector<int>::iterator it2 = v2.begin(); it2 != v2.end(); it2++)
{
    cout << *it2 << endl;
}

// result:
1
2
3

3. No Instantiation Used

Another solution is creating a pointer of that type (the container's type) and assign it to the first element of that object.by using this way, you are ready to go access other elements as well by using an iterator and go through the object. and the difference between 1 and this solution is that you won't instantiate a new object(the copy-to object)

4. A Quick Overview of solutions

The first way and the worst way and the most robust way is to choose 1st solution. if you choose the 2nd solution, you are actually doing a deep copy, that is well constructed(but it has its own drawbacks) if you want to choose the 3rd solution, you've done a shallow copying.(having some drawback)

5. First solution Pros and Cons

This way of doing a copy is not that much popular for pro problems. because it costs a big amount of time to run There is no danger of losting data on that container object.no dangling pointer will be generated at all(Robustness) In another level also it is not popular among programmers, its because its not a clean code

6. Second solution Pros and Cons

This way of doing a copy is not that much popular among professional programmers; because it costs a big amount of time to run There is no danger of losting data on that container object.no dangling pointer will be generated at all(Robustness) In another level also it is not popular among programmers, its because its not a clean code

7. Third solution Pros and Cons

This way of doing a copy is not that much popular among professional programmers; because it costs a big amount of time to run There is no danger of losting data on that container object.no dangling pointer will be generated at all(Robustness) In another level also it is not popular among programmers, its because its not a clean code.

the third solution is called shallow copying.as explained above there is a chance of catching an exception during runtime also the third solution is not actually copying.and its not moving too.this way may be used if needed on a particular problem.

Conclusion

Something to bare in mind for doing a solution for a particular in C++:
1. Is this application scalable later on
2. Is Performance(speed) the most important part of my program?
3. Can I update my code later in order to modify some container?
Before we go for analyzing solutions to this problem, i want to mention one point; and that is for almost every problem in C++ programming and in general in all programming languages the solution you choose is highly relevant to what kind of system you want this solution work for.

Thanks for reading. Any Comments,

Please let me know at adibh1996@gmail.com