← Back to C++ Course | Chapter 7: Pointers & References | Lesson 11 of 11

C++ के Smart Pointers

Smart pointers वे pointers हैं जो जब कोई उनकी point की चीज़ को नहीं चाहता तो अपने आप साफ हो जाते हैं। यह एक borrowed book जैसा है जो खुद को library को वापस कर देती है।
Syntax
cpp
#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

cpp
#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

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

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

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

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

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

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

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

cpp
#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;
}
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. manually new और delete इस्तेमाल करना जबकि result को एक smart pointer में भी store करना, एक double free cause करते हुए।
  2. एक unique_ptr copy करना, जो allowed नहीं है।
  3. 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 करने में मदद करते हैं।

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.