Difference Between Shallow Copy and Deep Copy in C++ with Examples
Shallow Copy and Deep Copy are two approaches to copying objects in C++. Both can copy the data members of an object, but they differ significantly in how they handle pointers and dynamically allocated memory. Understanding the difference between shallow copy and deep copy is important when working with pointers, dynamic memory allocation, copy constructors, and object-oriented programming in C++.
C++ में Shallow Copy और Deep Copy objects को copy करने के दो तरीके हैं। दोनों किसी object के data members को copy कर सकते हैं, लेकिन pointers और dynamically allocated memory को handle करने के तरीके में इनके बीच महत्वपूर्ण अंतर होता है। C++ में pointers, dynamic memory allocation, copy constructors और object-oriented programming के साथ काम करते समय Shallow Copy और Deep Copy के बीच अंतर समझना महत्वपूर्ण है।
What Is Shallow Copy in C++?
A shallow copy copies the data members of an object member by member. When a data member is a pointer, its address is copied rather than the actual dynamically allocated data. As a result, the original object and the copied object may point to the same memory location. Therefore, modifying the data through one pointer can affect what is observed through the other pointer.
Shallow Copy में object के data members को member by member copy किया जाता है। यदि कोई data member pointer है, तो actual dynamically allocated data के बजाय उसका address copy होता है। इसके परिणामस्वरूप original object और copied object एक ही memory location को point कर सकते हैं। इसलिए एक pointer के माध्यम से data modify करने पर दूसरे pointer के माध्यम से दिखाई देने वाला data भी प्रभावित हो सकता है।
Example of Shallow Copy:
#include <iostream>using namespace std;class Demo {public:int* ptr;Demo(int value) {ptr = new int(value);}};int main() {Demo obj1(100);// Memberwise copyDemo obj2 = obj1;cout << "Original Object: " << *obj1.ptr << endl;cout << "Copied Object: " << *obj2.ptr << endl;*obj2.ptr = 500;cout << "\nAfter modifying copied object:\n";cout << "Original Object: " << *obj1.ptr << endl;cout << "Copied Object: " << *obj2.ptr << endl;return 0;}
Output:
Original Object: 100Copied Object: 100After modifying copied object:Original Object: 500Copied Object: 500
Explanation: The implicitly generated copy constructor performs memberwise copying. In this example, the pointer address is copied, so both objects point to the same dynamically allocated integer. Changing the value through obj2.ptr also changes the value observed through obj1.ptr.
व्याख्या: Compiler द्वारा implicitly generated copy constructor memberwise copying करता है। इस उदाहरण में pointer का address copy होता है, इसलिए दोनों objects एक ही dynamically allocated integer को point करते हैं। obj2.ptr के माध्यम से value बदलने पर obj1.ptr के माध्यम से दिखाई देने वाली value भी बदल जाती है।
Note: This example intentionally does not define a destructor. If both objects independently deleted the same shared pointer, the program could encounter undefined behavior due to double deletion.
ध्यान दें: इस उदाहरण में जानबूझकर destructor define नहीं किया गया है। यदि दोनों objects एक ही shared pointer को independently delete करते, तो double deletion के कारण program में undefined behavior हो सकता था।
What Is Deep Copy in C++?
A deep copy creates an independent copy of an object. When the object contains a pointer to dynamically allocated memory, deep copying allocates separate memory and copies the actual data into it.
Deep Copy किसी object की स्वतंत्र copy बनाता है। जब object में dynamically allocated memory का pointer होता है, तब Deep Copy अलग memory allocate करता है और उसमें actual data copy करता है।
As a result, the original object and copied object have separate memory locations. Changes made to one object do not affect the data owned by the other object.
इसके परिणामस्वरूप original object और copied object की memory locations अलग-अलग होती हैं। एक object में किए गए changes दूसरे object के data को प्रभावित नहीं करते।
Example of Deep Copy:
#include <iostream>using namespace std;class Demo {private:int* ptr;public:Demo(int value) {ptr = new int(value);}// Deep Copy ConstructorDemo(const Demo& obj) {ptr = new int(*obj.ptr);}// Destructor~Demo() {delete ptr;}void setValue(int value) {*ptr = value;}void display() const {cout << *ptr << endl;}};int main() {Demo obj1(100);// Deep copyDemo obj2 = obj1;cout << "Original Object: ";obj1.display();cout << "Copied Object: ";obj2.display();obj2.setValue(500);cout << "\nAfter modifying copied object:\n";cout << "Original Object: ";obj1.display();cout << "Copied Object: ";obj2.display();return 0;}
Output:
Original Object: 100Copied Object: 100After modifying copied object:Original Object: 100Copied Object: 500
Explanation: The user-defined copy constructor allocates a new memory location using new int(*obj.ptr). It copies the actual integer value rather than copying the pointer address. Therefore, both objects maintain separate copies of the data.
व्याख्या: User-defined copy constructor new int(*obj.ptr) की सहायता से नई memory location allocate करता है। यह pointer address copy करने के बजाय actual integer value copy करता है। इसलिए दोनों objects data की अलग-अलग copies रखते हैं।
Difference Between Shallow Copy and Deep Copy in C++
The following table explains the major differences between shallow copy and deep copy in C++.
निम्न table C++ में Shallow Copy और Deep Copy के बीच मुख्य अंतर समझाती है।
| Basis | Shallow Copy | Deep Copy |
|---|---|---|
| Meaning | Copies data members member by member. | Creates independent copies of owned data. |
| Pointer handling | Copies pointer addresses. | Allocates separate memory and copies pointed-to data. |
| Memory location | Pointer members may refer to the same memory. | Owned dynamically allocated data has separate memory. |
| Data modification | Changes through one shared pointer may affect the other object. | Changes to one object's independently owned data do not affect the other. |
| Memory allocation | Does not allocate separate memory for the pointed-to resource as part of pointer copying. | Allocates new memory for the copied resource. |
| Memory ownership | Can create shared ownership unintentionally. | Can provide independent ownership when correctly implemented. |
| Copy constructor | Compiler-generated copy constructor performs memberwise copying. | Often requires a user-defined copy constructor for owning raw pointers. |
| Copy assignment | Default memberwise assignment can copy pointer addresses. | Requires suitable copy assignment logic for independently owned resources. |
| Memory management | Can cause dangling pointers, double deletion, or ownership problems if resources are managed incorrectly. | Helps avoid shared-ownership problems when implemented correctly. |
| Complexity | Usually simpler to perform. | Requires additional memory-management logic for raw resources. |
| Use case | Suitable when memberwise copying is appropriate, such as non-owning pointers or shared references with a defined lifetime. | Suitable when objects need independent copies of dynamically allocated resources. |
Shallow Copy और Deep Copy में अंतर (हिन्दी)
| आधार | Shallow Copy | Deep Copy |
|---|---|---|
| अर्थ | Data members को member by member copy करता है। | Owned data की स्वतंत्र copies बनाता है। |
| Pointer Handling | Pointer addresses copy करता है। | अलग memory allocate करके pointed-to data copy करता है। |
| Memory Location | Pointer members एक ही memory को refer कर सकते हैं। | Independently owned dynamic data की memory अलग होती है। |
| Data Modification | Shared pointer के माध्यम से किया गया change दूसरे object को प्रभावित कर सकता है। | एक object के independently owned data में किया गया change दूसरे को प्रभावित नहीं करता। |
| Memory Allocation | Pointer copy करते समय pointed-to resource के लिए अलग memory allocate नहीं करता। | Copied resource के लिए नई memory allocate करता है। |
| Memory Ownership | अनजाने में shared ownership की समस्या पैदा कर सकता है। | सही implementation होने पर independent ownership प्रदान करता है। |
| Copy Constructor | Compiler-generated copy constructor memberwise copying करता है। | Owning raw pointers के लिए अक्सर user-defined copy constructor की आवश्यकता होती है। |
| Copy Assignment | Default memberwise assignment pointer addresses copy कर सकता है। | Independent ownership के लिए suitable copy assignment logic आवश्यक है। |
| Memory Management | गलत resource management से dangling pointers, double deletion या ownership problems हो सकती हैं। | सही implementation होने पर shared ownership की समस्याओं से बचने में मदद करता है। |
| Complexity | सामान्यतः सरल होता है। | Raw resources के लिए अतिरिक्त memory-management logic की आवश्यकता होती है। |
| उपयोग | जब memberwise copying उचित हो, जैसे non-owning pointers या defined lifetime वाले shared references। | जब objects को dynamically allocated resources की independent copies चाहिए हों। |
Memory Representation: Shallow Copy vs Deep Copy
The following conceptual diagrams illustrate how pointer-based objects refer to memory after copying.
निम्न conceptual diagrams बताते हैं कि copying के बाद pointer-based objects memory को किस प्रकार refer करते हैं।
1. Shallow Copy
Original Object Copied Object+-------------+ +-------------+| ptr | | ptr |+------+------+ +------+------+| |+-------------+--------------+|v+---------------+| Dynamic Memory|| Value || 100 |+---------------+
In shallow copying, both pointer members may refer to the same dynamically allocated memory.
Shallow Copy में दोनों pointer members एक ही dynamically allocated memory को refer कर सकते हैं।
2. Deep Copy
Original Object Copied Object+-------------+ +-------------+| ptr | | ptr |+------+------+ +------+------+| |v v+---------------+ +---------------+| Dynamic Memory| | Dynamic Memory|| Value | | Value || 100 | | 100 |+---------------+ +---------------+
In deep copying, each object points to its own dynamically allocated memory containing an independent copy of the data.
Deep Copy में प्रत्येक object अपनी अलग dynamically allocated memory को point करता है, जिसमें data की स्वतंत्र copy मौजूद होती है।
Advantages and Disadvantages
Both copying approaches have their own characteristics. The appropriate approach depends on how the class manages its resources and what copying behavior is required.
दोनों copying approaches की अपनी विशेषताएँ हैं। सही approach का चुनाव इस बात पर निर्भर करता है कि class अपने resources को कैसे manage करती है और किस प्रकार की copying आवश्यक है।
Advantages of Shallow Copy
Simple Copying: Memberwise copying is straightforward and generally requires less code.
सरल Copying: Memberwise copying आसान होती है और सामान्यतः कम code की आवश्यकता होती है।
Less Memory Allocation: It does not create a separate allocation for each copied pointer.
कम Memory Allocation: प्रत्येक copied pointer के लिए अलग memory allocation नहीं बनाता।
Useful for Non-Owning Pointers: It can be appropriate when pointers are non-owning and the referenced object's lifetime is managed elsewhere.
Non-Owning Pointers में उपयोगी: जब pointers केवल reference करते हैं और referenced object की lifetime कहीं और manage होती है, तब यह उपयुक्त हो सकता है।
Disadvantages of Shallow Copy
Shared Data: Multiple objects may refer to the same memory.
Shared Data: कई objects एक ही memory को refer कर सकते हैं।
Unintended Modification: Changes through one pointer may affect data observed through another object.
अनचाहे Changes: एक pointer के माध्यम से किए गए changes दूसरे object के माध्यम से दिखाई देने वाले data को प्रभावित कर सकते हैं।
Ownership Problems: If multiple objects incorrectly believe they own the same allocation, double deletion may occur.
Ownership Problems: यदि कई objects गलती से एक ही allocation को अपना resource मानते हैं, तो double deletion हो सकता है।
Advantages of Deep Copy
Independent Data: Each object can maintain its own copy of dynamically allocated data.
Independent Data: प्रत्येक object dynamically allocated data की अपनी अलग copy रख सकता है।
Safer Ownership: Each object can release its own resource independently.
सुरक्षित Ownership: प्रत्येक object अपने resource को independently release कर सकता है।
Independent Modification: Changes in one object do not affect the other object's separately owned data.
स्वतंत्र Modification: एक object में किए गए changes दूसरे object के अलग से owned data को प्रभावित नहीं करते।
Disadvantages of Deep Copy
More Memory Usage: Separate memory is allocated for copied resources.
अधिक Memory Usage: Copied resources के लिए अलग memory allocate की जाती है।
Additional Code: Raw-pointer resource management may require custom copy constructors and copy assignment operators.
अतिरिक्त Code: Raw-pointer resource management के लिए custom copy constructors और copy assignment operators की आवश्यकता हो सकती है।
More Complex Implementation: Memory allocation, copying, assignment, and destruction must be handled correctly.
अधिक जटिल Implementation: Memory allocation, copying, assignment और destruction को सही तरीके से handle करना आवश्यक होता है।
When Should You Use Shallow Copy or Deep Copy?
The choice between shallow copy and deep copy depends on the ownership of the data members and the intended behavior of the class.
Shallow Copy और Deep Copy के बीच चुनाव data members के ownership और class के intended behavior पर निर्भर करता है।
Use ordinary memberwise copying when the class contains independent value members or non-owning pointers whose lifetime is managed elsewhere.
Use deep copying when the class owns dynamically allocated resources and each copied object must own an independent copy.
Prefer standard library types such as
std::stringandstd::vectorinstead of manually managing dynamic memory whenever possible.Use smart pointers such as
std::unique_ptrwhen exclusive ownership is required. Remember thatstd::unique_ptris not copyable by default.जब class में independent value members या ऐसे non-owning pointers हों जिनकी lifetime कहीं और manage होती है, तब सामान्य memberwise copying का उपयोग किया जा सकता है।
जब class dynamically allocated resources की owner हो और प्रत्येक copied object को independent copy चाहिए, तब Deep Copy का उपयोग करें।
जहाँ संभव हो, dynamic memory को manually manage करने के बजाय
std::stringऔरstd::vectorजैसे standard library types को प्राथमिकता दें।जब exclusive ownership की आवश्यकता हो, तब
std::unique_ptrजैसे smart pointers का उपयोग करें। ध्यान रखें किstd::unique_ptrdefault रूप से copyable नहीं होता।
Frequently Asked Questions (FAQs)
1. What is the main difference between shallow copy and deep copy?
The main difference is that shallow copy copies pointer addresses, whereas deep copy creates separate memory and copies the actual pointed-to data when independent ownership is required.
मुख्य अंतर यह है कि Shallow Copy pointer addresses copy करता है, जबकि Deep Copy independent ownership के लिए अलग memory बनाकर actual pointed-to data copy करता है।
2. Which is better: shallow copy or deep copy?
Neither approach is universally better. The correct choice depends on the ownership model and the intended behavior of the class.
कोई भी approach हमेशा बेहतर नहीं होती। सही चुनाव class के ownership model और उसके intended behavior पर निर्भर करता है।
3. Does the default copy constructor perform shallow copy?
The implicitly generated copy constructor performs memberwise copying. For raw pointer members, this means copying the pointer addresses rather than allocating new memory for the pointed-to data.
Implicitly generated copy constructor memberwise copying करता है। Raw pointer members के मामले में इसका अर्थ है कि pointed-to data के लिए नई memory allocate करने के बजाय pointer addresses copy होते हैं।
4. Can shallow copy cause double deletion?
Yes. If two objects incorrectly treat the same dynamically allocated memory as their own and both destructors delete it, the program has undefined behavior.
हाँ। यदि दो objects गलती से एक ही dynamically allocated memory को अपना resource मानते हैं और दोनों destructors उसे delete करते हैं, तो program में undefined behavior होता है।
5. How is deep copy implemented in C++?
Deep copy is commonly implemented using a user-defined copy constructor and copy assignment operator that allocate separate memory and copy the actual data.
C++ में Deep Copy सामान्यतः user-defined copy constructor और copy assignment operator की सहायता से implement की जाती है, जो अलग memory allocate करके actual data copy करते हैं।
6. What is the difference between a copy constructor and a copy assignment operator?
A copy constructor creates a new object from an existing object, whereas a copy assignment operator copies data into an already existing object.
Copy constructor किसी existing object से नया object बनाता है, जबकि copy assignment operator पहले से मौजूद object में data copy करता है।
7. Is deep copy always required when using pointers?
No. Deep copy is needed when independent ownership and independent copies of the pointed-to resource are required. Non-owning pointers and intentionally shared resources may use different copying strategies.
नहीं। Deep Copy तब आवश्यक होती है जब pointed-to resource की independent ownership और independent copies चाहिए हों। Non-owning pointers और जानबूझकर shared resources के लिए दूसरी copying strategies का उपयोग किया जा सकता है।
Shallow Copy and Deep Copy are important concepts in C++ that help programmers understand how objects and dynamically allocated memory are copied. Shallow copy performs memberwise copying, while deep copy creates independent copies of owned resources.
Shallow Copy और Deep Copy C++ की महत्वपूर्ण अवधारणाएँ हैं, जो programmers को यह समझने में मदद करती हैं कि objects और dynamically allocated memory किस प्रकार copy होते हैं। Shallow Copy memberwise copying करती है, जबकि Deep Copy owned resources की independent copies बनाती है।
Understanding these concepts helps prevent common memory-management errors and supports the development of reliable C++ classes. For modern C++ programming, prefer RAII and standard library containers whenever possible instead of manually managing raw pointers.
इन concepts को समझने से सामान्य memory-management errors से बचने और reliable C++ classes विकसित करने में मदद मिलती है। Modern C++ programming में जहाँ संभव हो, raw pointers को manually manage करने के बजाय RAII और standard library containers को प्राथमिकता दें।
Comments
Post a Comment