Deep Copy V/S Shallow Copy

In programming, particularly in the context of object-oriented languages like C++, the concepts of shallow copy and deep copy are related to how data is duplicated or copied when dealing with objects or complex data structures.

Shallow Copy:

A shallow copy of an object is a new object that is a copy of the original object but shares references to the same dynamically allocated resources (such as memory) as the original. In other words, a shallow copy duplicates the top-level structure of the object but not the data elements themselves. If you modify a data element in the original object, the same element in the shallow copy will also be modified because they point to the same memory locations.

Shallow copies are often created implicitly when you use the copy constructor or the assignment operator without providing a custom implementation. By default, C++ classes perform a shallow copy.

Here's an example illustrating a shallow copy:

#include <iostream>
#include <cstring>

class ShallowCopyExample {
public:
    char* data;
    
    // Constructor
    ShallowCopyExample(const char* str) {
        data = new char[strlen(str) + 1];
        strcpy(data, str);
    }

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

    ~ShallowCopyExample() {
        delete[] data;
    }
};

int main() {
    ShallowCopyExample original("Hello, World!");
    ShallowCopyExample shallowCopy = original; // Shallow copy
    
    std::cout << "Original: " << original.data << std::endl;
    std::cout << "Shallow Copy: " << shallowCopy.data << std::endl;
    
    // Modify the data in the original object
    original.data[0] = 'G';
    
    std::cout << "Original: " << original.data << std::endl;
    std::cout << "Shallow Copy: " << shallowCopy.data << std::endl; // Also modified
    
    return 0;
}

In this example, both original and shallowCopy share the same data array, which leads to unintended behavior when we modify original.

Deep Copy:

A deep copy of an object is a new object that is a completely independent duplicate of the original object, including all the data elements. When you perform a deep copy, you allocate new memory for the copied object's data and copy the data from the original object to the new one. As a result, changes made to one object do not affect the other.

Creating a deep copy often involves implementing a custom copy constructor and assignment operator (or using libraries like std::vector that handle deep copies automatically).

Here's an example illustrating a deep copy:

#include <iostream>
#include <cstring>

class DeepCopyExample {
public:
    char* data;

    DeepCopyExample(const char* str) {
        data = new char[strlen(str) + 1];
        strcpy(data, str);
    }

    // Custom copy constructor (deep copy)
    DeepCopyExample(const DeepCopyExample& other) {
        data = new char[strlen(other.data) + 1];
        strcpy(data, other.data);
    }

    ~DeepCopyExample() {
        delete[] data;
    }
};

int main() {
    DeepCopyExample original("Hello, World!");
    DeepCopyExample deepCopy = original; // Deep copy
    
    std::cout << "Original: " << original.data << std::endl;
    std::cout << "Deep Copy: " << deepCopy.data << std::endl;
    
    // Modify the data in the original object
    original.data[0] = 'G';
    
    std::cout << "Original: " << original.data << std::endl;
    std::cout << "Deep Copy: " << deepCopy.data << std::endl; // Not modified

    return 0;
}

In this example, the deep copy ensures that changes to the original object do not affect the deepCopy object because they have their own independent copies of the data array.

In summary, the choice between shallow copy and deep copy depends on your specific requirements. Shallow copies are more memory-efficient but can lead to unintended side effects when objects share data. Deep copies provide data isolation but may be less efficient in terms of memory and processing time when dealing with large objects or complex data structures.

Last updated

Was this helpful?