Types of Copying in C++, Shallow Copy, Deep Copy

When objects contain pointer data members, it is important to understand shallow copying and deep copying.

जब किसी class में pointer data members होते हैं, तब shallow copy और deep copy को समझना आवश्यक होता है।

Shallow Copy

A shallow copy copies the values of data members as they are. If a data member is a pointer, only the pointer value (memory address) is copied, not the dynamically allocated data itself.

Shallow Copy में data members की values copy होती हैं। यदि कोई data member pointer है, तो उसका memory address copy होता है, न कि उस address पर मौजूद dynamically allocated data।

Example:

#include <iostream>
using namespace std;

class Demo
{
public:
    int *ptr;

    Demo(int value)
    {
        ptr = new int(value);
    }

    ~Demo()
    {
        delete ptr;
    }
};

int main()
{
    Demo d1(100);

    cout << "Value: " << *d1.ptr << endl;

    return 0;
}

Output:

Value: 100

This example demonstrates dynamic memory allocation. If the compiler-generated copy constructor is used to copy d1, the pointer address is copied, so both objects would point to the same allocated memory.

ऐसी स्थिति में दोनों objects एक ही memory location को point कर सकते हैं। यदि दोनों destructors उसी pointer को delete करते हैं, तो double deletion जैसी गंभीर समस्या उत्पन्न हो सकती है।

Deep Copy

A deep copy creates separate memory for the new object and copies the actual data into that memory.

Deep Copy में नए object के लिए अलग memory allocate की जाती है और original object का actual data उसमें copy किया जाता है।

Example:

#include <iostream>
using namespace std;

class Demo
{
private:
    int *ptr;

public:
    Demo(int value)
    {
        ptr = new int(value);
    }

    Demo(const Demo &obj)
    {
        ptr = new int(*obj.ptr);
    }

    ~Demo()
    {
        delete ptr;
    }

    void display()
    {
        cout << "Value: " << *ptr << endl;
    }
};

int main()
{
    Demo d1(100);

    Demo d2(d1);

    cout << "Original Object:" << endl;
    d1.display();

    cout << "Copied Object:" << endl;
    d2.display();

    return 0;
}

Output:

Original Object:
Value: 100
Copied Object:
Value: 100

Explanation:

  1. The constructor dynamically allocates memory for ptr.

  2. The copy constructor allocates separate memory for the new object.

  3. It copies the value stored at obj.ptr into the newly allocated memory.

  4. Both objects now manage separate memory locations.

  5. The destructor releases the memory owned by each object.

व्याख्या:

  1. Constructor, ptr के लिए dynamically memory allocate करता है।

  2. Copy constructor नए object के लिए अलग memory allocate करता है।

  3. Original pointer द्वारा store की गई value को नई memory में copy किया जाता है।

  4. दोनों objects की memory अलग-अलग होती है।

  5. Destructor प्रत्येक object की memory को release करता है।

Note: This is a basic deep-copy example. If a class manages raw owning pointers, it should also define or appropriately disable copy assignment and consider move operations. Modern C++ generally prefers standard library types such as std::string, std::vector and smart pointers to manage resources safely.

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)