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

C++ में shared_ptr और weak_ptr

एक shared_ptr कई owners को एक चीज़ share करने देता है और last owner के जाने पर clean up करता है, last person की lights off करने की तरह। एक weak_ptr बिना owning किए watch करता है।
Syntax
cpp
#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

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

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

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(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;
}

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

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

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

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

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

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> 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;
}
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. circular shared_ptr references बनाना ताकि objects कभी free न हों।
  2. same raw pointer से दो separate shared_ptr बनाना, जो एक double delete cause करता है।
  3. पहले lock() call किए बिना एक weak_ptr सीधे इस्तेमाल करना।
चैप्टर सारांश
  • Memory management यह cover करता है कि programs memory कैसे allocate और release करते हैं।
  • new और delete memory बनाते और free करते हैं, dynamic arrays समेत, और free करना भूल जाना memory leaks cause करता है।
  • unique_ptr, shared_ptr, और weak_ptr automatically memory manage करते हैं।
🔒

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.