C++ में shared_ptr और weak_ptr
In this page:
#include <memory>
std::shared_ptr<data_type> pointer_name = std::make_shared<data_type>(value);
std::shared_ptr<data_type> copy = pointer_name; // shared ownership
std::weak_ptr<data_type> observer = pointer_name; // does not own
shared_ptr Basics
std::shared_ptr कई pointers को same dynamically allocated object jointly own करने देता है, एक internal reference count से track होते हुए।
Object सिर्फ तभी delete होता है जब वह count zero पर आता है, मतलब scope से बाहर जाने वाला last shared_ptr ही actually memory free करता है।
उदाहरण: shared_ptr Basics
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a shared_ptr: a reference-counted smart pointer
std::shared_ptr<int> ptr(new int(5));
// Print to the console with cout
std::cout << *ptr << " count=" << ptr.use_count() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
shared_ptr Copy करना
unique_ptr के उलट, एक shared_ptr freely copy किया जा सकता है -- हर copy shared reference count increment करता है, और हर destroyed copy इसे decrement करता है।
यह shared_ptr को सही tool बनाता है जब एक object की lifetime genuinely program के कई parts में share होनी चाहिए, सिर्फ एक से owned होने की बजाय।
उदाहरण: Copying shared_ptr
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a shared_ptr: a reference-counted smart pointer
std::shared_ptr<int> ptr1(new int(5));
// Use a shared_ptr: a reference-counted smart pointer
std::shared_ptr<int> ptr2 = ptr1;
// Print to the console with cout
std::cout << ptr1.use_count() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
make_shared
std::make_shared<T>(args) object और इसका reference-counting control block साथ में एक single heap allocation में allocate करता है, इसे एक separately new'd raw pointer से एक shared_ptr construct करने से faster और ज़्यादा exception-safe दोनों बनाते हुए।
उदाहरण: make_shared
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a shared_ptr: a reference-counted smart pointer
auto ptr = std::make_shared<int>(42);
// Print to the console with cout
std::cout << *ptr << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Circular Dependency Problem
अगर दो objects एक-दूसरे को shared_ptrs holds करते हैं, उनके reference counts कभी zero तक नहीं पहुँच सकते -- हर एक दूसरे को indefinitely alive रखता है, भले ही अब कुछ भी बाहर pair को reference न करे।
यह reference cycle एक genuine memory leak है जिसे अकेला shared_ptr detect या resolve नहीं कर सकता।
उदाहरण: The Circular Dependency Problem
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Define the type Node
class Node {
// Members below can be accessed from outside the class
public:
std::shared_ptr<Node> next;
~Node() { std::cout << "Node destroyed" << std::endl; }
};
// Program execution starts in main()
int main() {
// Use a shared_ptr: a reference-counted smart pointer
auto a = std::make_shared<Node>();
// Use a shared_ptr: a reference-counted smart pointer
auto b = std::make_shared<Node>();
a->next = b;
b->next = a;
// Print to the console with cout
std::cout << "Circular references created" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
weak_ptr Basics
std::weak_ptr एक shared_ptr से managed object को इसका reference count increment किए बिना observe करता है, जो exactly वह तरीका है जिससे आप ऊपर वाली circular-reference problem को break करते हैं।
क्योंकि यह जिस object की ओर point करता है वह पहले से gone हो सकता है, safely इसे access करने से पहले आपको एक temporary shared_ptr पाने के लिए lock() call करना ज़रूरी है।
उदाहरण: weak_ptr Basics
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a shared_ptr: a reference-counted smart pointer
std::shared_ptr<int> shared = std::make_shared<int>(42);
std::weak_ptr<int> weak = shared;
// Print to the console with cout
std::cout << shared.use_count() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- circular
shared_ptrreferences बनाना ताकि objects कभी free न हों। - same raw pointer से दो separate
shared_ptrबनाना, जो एक double delete cause करता है। - पहले
lock()call किए बिना एकweak_ptrसीधे इस्तेमाल करना।
- Memory management यह cover करता है कि programs memory कैसे allocate और release करते हैं।
newऔरdeletememory बनाते और free करते हैं, dynamic arrays समेत, और free करना भूल जाना memory leaks cause करता है।unique_ptr,shared_ptr, औरweak_ptrautomatically memory manage करते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: