Difference Between Shallow Copy and Deep Copy in C++

1. Introduction

In C++ programming, understanding the difference between shallow copy and deep copy is crucial for managing memory and object copying. A shallow copy copies all member field values, while a deep copy duplicates everything the original object has, including pointing to new memory locations for the pointers.

2. Key Points

1. A shallow copy copies the values of the object members to another object, including the addresses of pointers.

2. A deep copy creates a copy of the object and all objects it refers to, duplicating any dynamic memory pointed to by the object.

3. Shallow copying is faster but can lead to issues like double frees and dangling pointers.

4. Deep copying is safer as it avoids issues with shared memory but can be more memory intensive and slower.

3. Differences

Shallow Copy Deep Copy
Copies object's member values as they are, including memory addresses. Creates copies of the objects pointed to by the member pointers.
Can lead to problems like double frees. Avoids problems associated with shared memory.
Less memory intensive but risky with pointers. More memory intensive but safer with pointers.

4. Example

#include <iostream>
using namespace std;

class MyClass {
public:
    int *data;
    MyClass(int d) { data = new int(d); }
    ~MyClass() { delete data; }

    // Implementing a shallow copy
    MyClass(const MyClass &src) {
        data = src.data;
    }

    // Implementing a deep copy
    MyClass deepCopy() {
        MyClass copy(*data);
        return copy;
    }
};

int main() {
    MyClass original(10);

    // Shallow copy
    MyClass shallowCopy = original;
    cout << "Shallow Copy data: " << *shallowCopy.data << endl;

    // Deep copy
    MyClass deepCopy = original.deepCopy();
    cout << "Deep Copy data: " << *deepCopy.data << endl;

    return 0;
}

Output:

Shallow Copy data: 10
Deep Copy data: 10

Explanation:

1. In the shallow copy, shallowCopy and original share the same data pointer.

2. In the deep copy, deepCopy has its own copy of data, separate from original.

5. When to use?

- Use shallow copy for simple objects without pointers or when performance is critical and you manage memory carefully.

- Use deep copy when working with objects that contain pointers to prevent issues related to shared memory, such as double deletion or dangling pointers.

Related C++/CPP Posts:

Difference Between Struct and Class in C++

Difference Between Pointer and Reference in C++

Difference Between null and nullptr in C++

Difference Between Array and Vector in C++

Difference Between const and constexpr in C++

Difference Between List and Vector in C++

Difference Between C and C++

Difference Between Function Overloading and Operator Overloading in C++

Difference Between Array and List in C++

Difference Between a While Loop and a Do-While Loop in C++

Difference Between new and malloc C++

Virtual Function vs Pure Virtual Function in C++

Compile Time Polymorphism vs Runtime Polymorphism in C++

Difference Between Shallow Copy and Deep Copy in C++

Difference Between Stack and Heap in C++

Copy Constructor vs Parameterized Constructor in C++

Comments