C++ के Smart Pointers
In this page:
#include <memory>
std::unique_ptr<data_type> pointer_name = std::make_unique<data_type>(value);
std::shared_ptr<data_type> pointer_name = std::make_shared<data_type>(value);
Smart Pointers का Introduction
Smart pointers wrapper classes हैं जो dynamically allocated heap memory को automatically manage करते हैं, smart pointer object खुद के scope से बाहर जाते ही इसे release करते हुए।
यह delete manually call करने की ज़रूरत खत्म करता है और manual memory-management bugs का modern C++ जवाब है।
उदाहरण: Introduction to Smart Pointers
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int> ptr = std::make_unique<int>(10);
std::cout << *ptr << std::endl;
return 0;
} // memory released automatically here
Login to try C/C++/Java/PHP code in the editor
Unique Pointer
एक unique_ptr अपना dynamically allocated resource exclusively own करता है — इसे copy नहीं किया जा सकता, सिर्फ move किया जा सकता है — जो compile time पर guarantee करता है कि कभी दो pointers same memory delete करने की कोशिश नहीं करेंगे।
यह default choice है जब भी एक object का एक single, clear owner हो।
उदाहरण: Unique Pointer
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int> ptr1 = std::make_unique<int>(5);
std::unique_ptr<int> ptr2 = std::move(ptr1); // ownership transferred, not copied
std::cout << *ptr2 << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Shared Pointer
एक shared_ptr कई pointers को internally कितने shared_ptr instances currently इसकी ओर point करते हैं इसका एक reference count रखकर same memory block को जोड़े में own करने देता है।
Memory सिर्फ तभी free होती है जब इसे reference करने वाला बिल्कुल आखिरी shared_ptr destroy हो, shared ownership को safe बनाते हुए।
उदाहरण: Shared Pointer
// 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 = std::make_shared<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 << "Use count: " << 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
Weak Pointer
एक weak_ptr एक shared_ptr द्वारा managed एक object का non-owning reference रखता है, मतलब यह reference count को affect नहीं करता।
यह shared_ptrs के बीच circular references तोड़ने के लिए essential है, जो अन्यथा एक-दूसरे की memory हमेशा के लिए alive रखेंगे और एक silent leak cause करेंगे।
उदाहरण: Weak Pointer
#include <iostream>
#include <memory>
int main() {
std::shared_ptr<int> shared = std::make_shared<int>(5);
std::weak_ptr<int> weak = shared; // doesn't affect the reference count
std::cout << "Use count still: " << shared.use_count() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Safe Factory Functions
Smart pointers safely और efficiently बनाने के लिए, सीधे new call करने की बजाय factory functions make_unique और make_shared पसंद करें।
वे memory allocation और object construction को एक single step में combine करते हैं, जो overhead कम करता है और अलग new calls की एक subtle exception-safety pitfall avoid करता है।
उदाहरण: Safe Factory Functions
#include <iostream>
#include <memory>
int main() {
auto ptr = std::make_unique<int>(42); // preferred over: new int(42)
std::cout << *ptr << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- manually
newऔरdeleteइस्तेमाल करना जबकि result को एक smart pointer में भी store करना, एक double free cause करते हुए। - एक
unique_ptrcopy करना, जो allowed नहीं है। - same raw pointer से दो
shared_ptrबनाना।
- Pointers memory addresses store करते हैं और जिस value की ओर point करते हैं उसे पढ़ने या modify करने के लिए dereference किए जाते हैं।
- Pointer arithmetic, और pointers, arrays, और functions के बीच links, आपको memory के साथ सीधे काम करने देते हैं।
- References variables के aliases हैं, और smart pointers memory safely manage करने में मदद करते हैं।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: