एक पॉइंटर का उपयोग दूसरे वेरिएबल के एड्रेस को स्टोर करने के लिए किया जाता है। दूसरे शब्दों में, एक पॉइंटर प्रोग्राम (हीप मेमोरी) के बाहर एक संसाधन की जानकारी निकालता है।
हम संसाधन की एक प्रति का उपयोग करते हैं, और परिवर्तन करने के लिए, यह कॉपी किए गए संस्करण में किया जाता है। मूल संसाधन की सामग्री को बदलने के लिए, smart pointers का उपयोग किया जाता है।
Problems faced with normal pointers in C++ In Hindi
नीचे एक उदाहरण दिया गया है जो normal pointers के साथ समस्या को दर्शाता है।
दो डेटा सदस्यों (लंबाई और चौड़ाई) के साथ एक वर्ग आयत बनाया। एक फ़ंक्शन fun() गतिशील रूप से आयत का एक ऑब्जेक्ट बनाता है।
जब फ़ंक्शन fun() समाप्त होता है, तो ऑब्जेक्ट p नष्ट हो जाता है। चूंकि हमने p को नहीं हटाया है, मेमोरी आवंटित रहती है, और यह आवंटित संसाधन अन्य चर के लिए उपलब्ध नहीं होगा।
main () में, हम एक अनंत लूप निष्पादित करते हैं जो p ऑब्जेक्ट बनाते रहेंगे और संसाधन आवंटित करेंगे। यह समस्या memory leak में बदल जाती है जिसका समाधान स्मार्ट पॉइंटर्स है।
- #include <iostream>
- using namespace std;
- class Rectangle { // created a class Rectangle
- private:
- int length; // data member as length of rectangle
- int breadth; // data member as breadth of rectangle
- };
- void fun() // the function to indicate the problem with normal pointer
- {
- Rectangle* p = new Rectangle(); // Create a dynamic object p
- }
- int main()
- {
- // Infinite Loop
- while (1) { // Run an infinite loop that will allocate p
- fun();
- }
- }
Smart pointers in C++
हम स्मार्ट पॉइंटर्स को इस तरह लागू करेंगे कि वे अप्रयुक्त संसाधनों की मेमोरी को छोड़ दें।
एक पॉइंटर, ओवरलोडेड ऑपरेटर्स (->, *) और डिस्ट्रक्टर्स के साथ एक क्लास बनाएं।
जब इसकी वस्तु दायरे से बाहर हो जाती है, तो विनाशक को स्वचालित रूप से बुलाया जाएगा, और स्वचालित रूप से गतिशील रूप से आवंटित स्मृति हटा दी जाएगी।
Example For Smart pointers in C++
- #include <iostream>
- using namespace std;
- class SmartPtr { // Create the class to implement smart Pointer
- int* ptr; // Actual pointer
- public:
- // Create an explicit constructor
- explicit SmartPtr(int* p = NULL) { ptr = p; }
- // Destructor to deallocate the resource used
- ~SmartPtr() { delete (ptr); }
- // Overloading dereferencing operator
- int& operator*() { return *ptr; }
- };
- int main()
- {
- SmartPtr ptr(new int());
- *ptr = 100;
- cout << *ptr;
- // We don't need to call delete ptr: when the object
- // ptr goes out of scope, the destructor for it is automatically
- // called, and destructor does delete ptr.
- return 0;
- }
OUTPUT :-
100
Types of smart pointers in C++
- Unique_ptr
इस प्रकार की वस्तु केवल एक ही वस्तु को संग्रहित करती है। किसी भिन्न ऑब्जेक्ट को assign करने के लिए, वर्तमान ऑब्जेक्ट को हटा दिया जाता है।
- shared_ptr
Shared_ptr में, एक ही समय में एक से अधिक ऑब्जेक्ट एक ही पॉइंटर को इंगित कर सकते हैं। use_count() विधि का उपयोग करके ऑब्जेक्ट को निरूपित करने के लिए एक संदर्भ काउंटर बनाए रखा जाता है।
- #include <iostream>
- #include <memory>
- using namespace std;
- class Rectangle { // Create the class
- // Data members
- int length; // length of rectangle
- int breadth; // breadth of rectangle
- public:
- Rectangle(int l, int b)
- { // parameterised constructor
- length = l;
- breadth = b;
- }
- int area()
- { // calculate area of rectangle
- return length * breadth; // return area
- }
- };
- int main()
- {
- shared_ptr<Rectangle> P1(new Rectangle(20, 5)); // create shared //ptr P1
- // This'll print 100
- cout << P1->area() << endl;
- // Create shared ptr P2
- shared_ptr<Rectangle> P2;
- P2 = P1;
- // This'll print 100
- cout << P2->area() << endl;
- // This'll now not give an error,
- cout << P1->area() << endl; // prints 100
- // reference counter of P2 is 2
- cout << P1.use_count() << endl; // prints 2
- return 0;
- }
OUTPUT :-
- Weak_ptr
Weak_ptr साझा सूचक के समान है। अंतर यह है कि यह एक संदर्भ काउंटर नहीं रखता है और सूचक पर वस्तु का कोई मजबूत पकड़ नहीं है। इस गुण के परिणामस्वरूप गतिरोध हो सकता है क्योंकि विभिन्न ऑब्जेक्ट पॉइंटर को होल्ड करने का प्रयास करेंगे।
No comments:
Post a Comment