Deep Copy and Shallow Copy

In C++, deep copy and shallow copy are two different approaches to copying objects.

Shallow Copy: A shallow copy creates a new object and copies the values of the members from the original object to the new object. If the members of the object are pointers, the shallow copy will copy the address of the pointer without creating a new copy of the pointed data. Both the original object and the copied object will point to the same memory locations, which can lead to problems if one object modifies the data.

Here’s an example of shallow copy in C++:

#include <iostream>

class ShallowCopyExample {
public:
    int* data;

    ShallowCopyExample(int val) {
        data = new int;
        *data = val;
    }

    // Shallow copy constructor
    ShallowCopyExample(const ShallowCopyExample& other) {
        data = other.data;
    }

    ~ShallowCopyExample() {
        delete data;
    }
};

int main() {
    ShallowCopyExample original(5);
    ShallowCopyExample shallowCopy(original);

    // Modifying the data through original or shallowCopy affects both objects
    *original.data = 10;

    std::cout << *original.data << std::endl;      // Output: 10
    std::cout << *shallowCopy.data << std::endl;   // Output: 10

    return 0;
}

In the example above, the ShallowCopyExample class contains a dynamic integer data member data. When the copy constructor is invoked for shallow copy, it simply copies the pointer data from the original object to the new object. Both objects now point to the same memory location. Modifying the data through one object affects the other object because they share the same underlying memory.

Deep Copy: A deep copy creates a new object and copies both the values of the members and the data they point to. It ensures that the copied object has its own separate memory and modifications to one object do not affect the other.

Here’s an example of deep copy in C++:

#include <iostream>

class DeepCopyExample {
public:
    int* data;

    DeepCopyExample(int val) {
        data = new int;
        *data = val;
    }

    // Deep copy constructor
    DeepCopyExample(const DeepCopyExample& other) {
        data = new int;
        *data = *other.data;
    }

    ~DeepCopyExample() {
        delete data;
    }
};

int main() {
    DeepCopyExample original(5);
    DeepCopyExample deepCopy(original);

    // Modifying the data through original doesn't affect the deepCopy object
    *original.data = 10;

    std::cout << *original.data << std::endl;    // Output: 10
    std::cout << *deepCopy.data << std::endl;    // Output: 5

    return 0;
}

In the above example, the DeepCopyExample class also contains a dynamic integer data member data. The deep copy constructor allocates new memory for data in the copied object and copies the value from the original object. This way, the two objects have separate memory locations for data, and modifying one does not affect the other.

It’s important to note that in both cases, when using dynamically allocated memory like pointers, you should implement appropriate copy constructors, assignment operators, and destructors to avoid memory leaks and ensure proper resource management.

Leave a Reply

Your email address will not be published. Required fields are marked *