Shallow Copy in C++ - Definition, Working, Examples and Disadvantages
Shallow Copy is an important concept in C++ object-oriented programming. It occurs when one object is copied to another object by copying the values of its data members directly. When a class contains pointer members, a shallow copy copies the pointer address rather than creating a separate copy of the dynamically allocated memory. As a result, two objects may point to the same memory location. This can cause problems such as unintended data modification, dangling pointers, and double deletion.
C++ में Shallow Copy एक महत्वपूर्ण Object-Oriented Programming concept है। इसमें एक object के data members की values दूसरे object में copy की जाती हैं। यदि class में pointer members होते हैं, तो केवल pointer का address copy होता है, dynamically allocated memory की अलग copy नहीं बनती। इसके परिणामस्वरूप दोनों objects एक ही memory location को point कर सकते हैं। इससे data modification, dangling pointer और double deletion जैसी समस्याएँ उत्पन्न हो सकती हैं।
1. What Is Shallow Copy in C++?
Shallow Copy means copying an object's data members as they are. For ordinary data members such as int, float, and char, their values are copied. For pointer data members, only the memory address is copied.
Shallow Copy का अर्थ है किसी object के data members को सीधे दूसरे object में copy करना। int, float और char जैसे सामान्य data members की values copy होती हैं, जबकि pointer data members के मामले में केवल memory address copy होता है।
Example:
#include <iostream>using namespace std;class Demo {public:int value;};int main() {Demo obj1;obj1.value = 100;Demo obj2 = obj1;cout << "Object 1: " << obj1.value << endl;cout << "Object 2: " << obj2.value << endl;return 0;}
Output:
Object 1: 100Object 2: 100
Here, the value of obj1 is copied into obj2. Since the class contains only an integer member, both objects have independent copies of that value.
यहाँ obj1 की value obj2 में copy होती है। चूँकि class में केवल integer member है, इसलिए दोनों objects के पास अपनी-अपनी अलग value होती है।
2. Shallow Copy with Pointer Members
Shallow Copy becomes important when a class contains pointer members.
जब किसी class में pointer members होते हैं, तब Shallow Copy का प्रभाव अधिक महत्वपूर्ण हो जाता है।
Consider a class that dynamically allocates memory using the new operator. When its object is copied using the compiler-generated copy constructor, the pointer address is copied rather than the dynamically allocated data.
मान लीजिए कोई class new operator का उपयोग करके dynamically memory allocate करती है। जब compiler-generated copy constructor द्वारा उसका object copy किया जाता है, तो dynamically allocated data की अलग copy बनने के बजाय pointer का address copy होता है।
Example:
#include <iostream>using namespace std;class Demo {public:int* ptr;Demo(int value) {ptr = new int(value);}};int main() {Demo obj1(100);Demo obj2 = obj1;cout << "Object 1 Value: " << *obj1.ptr << endl;cout << "Object 2 Value: " << *obj2.ptr << endl;cout << "Object 1 Address: " << obj1.ptr << endl;cout << "Object 2 Address: " << obj2.ptr << endl;return 0;}
Sample Output:
Object 1 Value: 100Object 2 Value: 100Object 1 Address: 0x1234Object 2 Address: 0x1234
Note: Memory addresses shown above are illustrative. Actual addresses vary from system to system.
नोट: ऊपर दिए गए memory addresses केवल उदाहरण के लिए हैं। वास्तविक addresses अलग-अलग systems पर अलग हो सकते हैं।
Explanation:
obj1is created and memory is allocated dynamically usingnew.The pointer
obj1.ptrstores the address of the allocated memory.Demo obj2 = obj1;createsobj2using the compiler-generated copy constructor.The pointer address is copied from
obj1.ptrtoobj2.ptr.Both pointers refer to the same dynamically allocated integer.
हिन्दी में समझिए:
obj1create होने परnewसे dynamic memory allocate होती है।obj1.ptrउस memory का address store करता है।जब
Demo obj2 = obj1;किया जाता है, तो compiler-generated copy constructor execute होता है।Pointer का address
obj1.ptrसेobj2.ptrमें copy होता है।दोनों pointers एक ही dynamically allocated integer को refer करते हैं।
3. How Does Shallow Copy Work?
The following diagram illustrates shallow copying when a class contains a pointer member.
नीचे दिया गया diagram दिखाता है कि pointer member वाली class में Shallow Copy किस प्रकार कार्य करती है।
Object 1 Object 2+-----------+ +-----------+| ptr | | ptr |+-----------+ +-----------+| || |+-----------+-------------+|v+---------------+| Dynamic Memory|| Value || 100 |+---------------+
In shallow copying, both objects contain the same pointer address. Therefore, changing the value through one pointer also changes the value observed through the other pointer.
Shallow Copy में दोनों objects के pointers में एक ही memory address होता है। इसलिए एक pointer के माध्यम से value बदलने पर दूसरे pointer से भी बदली हुई value दिखाई देती है।
Example:
#include <iostream>using namespace std;class Demo {public:int* ptr;Demo(int value) {ptr = new int(value);}};int main() {Demo obj1(100);Demo obj2 = obj1;*obj2.ptr = 500;cout << "Object 1 Value: " << *obj1.ptr << endl;cout << "Object 2 Value: " << *obj2.ptr << endl;return 0;}
Output:
Object 1 Value: 500Object 2 Value: 500
Explanation:
Both objects refer to the same memory location. When the value is changed through obj2.ptr, the change is also visible through obj1.ptr.
दोनों objects एक ही memory location को access कर रहे हैं। इसलिए obj2.ptr के माध्यम से value बदलने पर obj1.ptr से भी बदली हुई value दिखाई देती है।
4. Disadvantages of Shallow Copy
Shallow Copy can create problems when objects manage dynamically allocated resources.
जब objects dynamically allocated resources को manage करते हैं, तब Shallow Copy कई प्रकार की समस्याएँ उत्पन्न कर सकती है।
1. Shared Memory
Two or more objects may point to the same dynamically allocated memory.
दो या दो से अधिक objects एक ही dynamically allocated memory को point कर सकते हैं।
2. Unintended Data Modification
Changing the data through one object may affect another object because both use the same memory.
एक object के माध्यम से data बदलने पर दूसरे object का data भी प्रभावित हो सकता है, क्योंकि दोनों एक ही memory का उपयोग करते हैं।
3. Double Deletion
If both objects attempt to delete the same dynamically allocated memory, the program has undefined behavior.
यदि दोनों objects एक ही dynamically allocated memory को delete करने का प्रयास करते हैं, तो program का behavior undefined हो जाता है।
4. Dangling Pointer
If one object deletes the shared memory, the pointer in the other object becomes dangling. Accessing the deleted memory through that pointer causes undefined behavior.
यदि एक object shared memory को delete कर देता है, तो दूसरे object का pointer dangling pointer बन जाता है। उस pointer के माध्यम से deleted memory को access करना undefined behavior उत्पन्न करता है।
हिन्दी में: Shallow Copy के कारण दो objects एक ही dynamic memory share कर सकते हैं। इससे data modification, double deletion और dangling pointer जैसी समस्याएँ उत्पन्न हो सकती हैं।
5. Shallow Copy and Copy Constructor
A copy constructor is a special constructor used to initialize an object from another object of the same class.
Copy Constructor एक special constructor होता है, जिसका उपयोग उसी class के किसी दूसरे object से नए object को initialize करने के लिए किया जाता है।
If a class does not define its own copy constructor, the compiler generally provides an implicitly declared copy constructor. For pointer members, this performs memberwise copying, which copies the pointer value rather than duplicating the pointed-to resource.
यदि class अपना copy constructor define नहीं करती है, तो compiler सामान्यतः implicitly declared copy constructor प्रदान करता है। Pointer members के मामले में यह memberwise copying करता है, जिसमें pointed-to resource की अलग copy बनाने के बजाय pointer की value यानी address copy होता है।
Syntax:
ClassName(const ClassName& obj);For example:
Demo obj2 = obj1;This statement initializes obj2 using the copy constructor.
इस statement में obj2 को obj1 की सहायता से initialize किया जाता है और copy constructor execute होता है।
6. How to Avoid Problems Caused by Shallow Copy?
When a class owns dynamically allocated memory, simply copying pointer addresses is usually unsafe.
जब कोई class dynamically allocated memory की ownership रखती है, तब केवल pointer addresses copy करना सामान्यतः सुरक्षित नहीं होता।
There are several ways to manage this problem:
Implement a proper deep-copy constructor.
Implement appropriate copy assignment behavior.
Use standard library containers and smart pointers.
Follow the Rule of Three or Rule of Five when managing resources manually.
इस समस्या को manage करने के लिए निम्नलिखित तरीके अपनाए जा सकते हैं:
Proper Deep Copy Constructor implement करें।
उचित copy assignment behavior implement करें।
Standard library containers और smart pointers का उपयोग करें।
यदि resources को manually manage कर रहे हैं, तो Rule of Three या Rule of Five का पालन करें।
A deep copy creates a separate resource for the copied object. This allows the two objects to manage their data independently.
Deep Copy में copied object के लिए अलग resource बनाया जाता है। इससे दोनों objects अपने data को स्वतंत्र रूप से manage कर सकते हैं।
7. Frequently Asked Questions (FAQs)
Q1. What is Shallow Copy in C++?
Shallow Copy copies the data members of an object directly. For pointer members, it copies the pointer address rather than the pointed-to memory.
Shallow Copy में object के data members सीधे copy किए जाते हैं। Pointer members के मामले में pointed-to memory की copy बनाने के बजाय pointer का address copy होता है।
Q2. What happens when a pointer is copied using Shallow Copy?
The pointer address is copied, so both pointers refer to the same memory location.
Pointer का address copy होता है, इसलिए दोनों pointers एक ही memory location को refer करते हैं।
Q3. Does Shallow Copy create separate memory?
Not for the dynamically allocated memory pointed to by a pointer member. Both objects may share the same allocation.
Pointer member द्वारा refer की जा रही dynamically allocated memory की अलग copy नहीं बनती। दोनों objects उसी memory allocation को share कर सकते हैं।
Q4. What is the main disadvantage of Shallow Copy?
It can cause shared-memory problems, unintended modifications, dangling pointers, and double deletion.
इससे shared memory, अनचाहे data modifications, dangling pointers और double deletion जैसी समस्याएँ हो सकती हैं।
Q5. Is the default copy constructor a Shallow Copy?
The compiler-generated copy constructor performs memberwise copying. For raw pointer members, this copies the address and therefore produces shallow-copy behavior.
Compiler-generated copy constructor memberwise copying करता है। Raw pointer members के लिए यह address copy करता है, इसलिए Shallow Copy जैसा behavior उत्पन्न होता है।
Q6. Is Shallow Copy always wrong in C++?
No. Shallow copying can be appropriate when shared ownership or non-owning pointers are intended and their lifetime is managed safely. It is dangerous when multiple objects incorrectly assume ownership of the same resource.
नहीं। यदि shared ownership या non-owning pointers का उद्देश्य हो और उनकी lifetime सुरक्षित रूप से manage की जा रही हो, तो Shallow Copy उपयोगी हो सकती है। समस्या तब आती है जब कई objects गलती से एक ही resource की ownership मान लेते हैं।
Q7. What is the difference between Shallow Copy and Deep Copy?
Shallow Copy copies pointer addresses, whereas Deep Copy creates independent copies of the resources being owned.
Shallow Copy में pointer addresses copy होते हैं, जबकि Deep Copy में owned resources की अलग-अलग copies बनाई जाती हैं।
Shallow Copy is a copying technique in which an object's data members are copied directly. It works normally for simple data members, but it can create problems when a class contains raw pointers that own dynamically allocated memory. Understanding Shallow Copy is essential before learning Deep Copy and copy constructors in C++. Modern C++ programmers should prefer safe resource-management techniques such as std::vector, std::string, and smart pointers whenever appropriate.
Shallow Copy एक ऐसी copying technique है जिसमें object के data members सीधे copy किए जाते हैं। यह सामान्य data members के लिए ठीक प्रकार से कार्य करती है, लेकिन जब किसी class में dynamically allocated memory को own करने वाले raw pointers होते हैं, तब समस्याएँ उत्पन्न हो सकती हैं। C++ में Deep Copy और Copy Constructor को समझने से पहले Shallow Copy को समझना आवश्यक है। Modern C++ में सुरक्षित resource management के लिए std::vector, std::string और smart pointers का उपयोग करना बेहतर होता है।
Comments
Post a Comment