← Back to C++ Course | Chapter 14: Memory Management | Lesson 5 of 6

C++ का unique_ptr

unique_ptr एक smart pointer है जो अकेला एक चीज़ owns करता है और खुद automatically clean up करता है, एक locker की इकलौती चाबी की तरह। आप इसे पास कर सकते हैं, लेकिन copy नहीं कर सकते।
Syntax
cpp
#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?

cpp
// 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;
}

कोई 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

cpp
// 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;
}

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()

cpp
// 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;
}

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

cpp
// 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;
}

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)

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक unique_ptr copy करना, जो compile नहीं होता; ownership transfer करने के लिए std::move इस्तेमाल करें।
  2. release() के बाद इसे free करने का ध्यान रखे बिना pointer इस्तेमाल करना।
  3. एक raw pointer से एक unique_ptr बनाना जिसे कोई दूसरा owner भी delete करता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.