C++ का unique_ptr
In this page:
#include <memory>
std::unique_ptr<data_type> pointer_name = std::make_unique<data_type>(value);
std::unique_ptr<data_type> other = std::move(pointer_name); // transfer ownership
unique_ptr क्या है?
std::unique_ptr, जो C++11 में introduce हुआ, एक raw pointer wrap करता है और guarantee करता है कि किसी भी वक्त object का exactly एक owner exist करे।
जब unique_ptr variable scope से बाहर जाता है, इसका destructor automatically managed object पर delete call करता है, इसलिए आपको इसे manually free करना कभी याद नहीं रखना पड़ता।
उदाहरण: What is unique_ptr?
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
std::unique_ptr<int> ptr(new 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
कोई Copying नहीं, सिर्फ Moving
क्योंकि एक unique_ptr exclusive ownership represent करता है, इसका copy constructor deleted है -- एक को copy करने की कोशिश एक compile error है।
ownership दूसरे unique_ptr को देने के लिए, आपको इसे explicitly std::move करना ज़रूरी है, जो underlying pointer transfer करता है और access duplicate करने की बजाय original को empty छोड़ता है।
उदाहरण: No Copying, Only Moving
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
std::unique_ptr<int> ptr1(new int(5));
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
std::unique_ptr<int> ptr2 = std::move(ptr1);
// Print to the console with cout
std::cout << *ptr2 << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
reset() और release()
reset() call करना तुरंत currently owned object delete करता है और optionally आपके pass किए एक नए को manage करना शुरू करता है।
इसकी बजाय release() call करना raw pointer आपको वापस देता है और इसे पूरी तरह भूल जाता है, इसलिए आप इसे manually delete करने के responsible हो जाते हैं -- older APIs के साथ interface करते वक्त useful जो raw pointers expect करती हैं।
उदाहरण: reset() and release()
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
std::unique_ptr<int> ptr(new int(5));
// Allocate an object on the heap with new
ptr.reset(new int(10));
// 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
Arrays के साथ unique_ptr
dynamically allocated arrays के लिए, unique_ptr<T> की बजाय unique_ptr<T[]> declare करें -- यह specialization element access के लिए operator[] overload करता है और scope से बाहर जाने पर delete की बजाय automatically delete[] call करता है, यह matching करते हुए कि array originally कैसे allocated हुआ था।
उदाहरण: unique_ptr with Arrays
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
std::unique_ptr<int[]> arr(new int[3]{1, 2, 3});
// Print to the console with cout
std::cout << arr[1] << 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_unique (C++14)
std::make_unique<T>(args) एक unique_ptr बनाने का recommended तरीका है, क्योंकि यह object construct करता है और इसे एक ही expression में wrap करता है -- एक bare new से बचते हुए जो allocation और wrapping के बीच एक exception throw होने पर leak कर सकता था।
यह C++14 में make_shared से match करने के लिए add हुआ, जो C++11 से exist कर रहा था।
उदाहरण: make_unique (C++14)
// Include std::cout and std::cin
#include <iostream>
// Include smart pointers
#include <memory>
// Program execution starts in main()
int main() {
// Use a unique_ptr: a smart pointer that owns the object and frees it automatically
auto ptr = std::make_unique<int>(99);
// 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
- एक
unique_ptrcopy करना, जो compile नहीं होता; ownership transfer करने के लिएstd::moveइस्तेमाल करें। release()के बाद इसे free करने का ध्यान रखे बिना pointer इस्तेमाल करना।- एक raw pointer से एक
unique_ptrबनाना जिसे कोई दूसरा owner भी delete करता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: