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

C++ में Memory Leaks

एक memory leak तब होता है जब एक program memory उधार लेता है और कभी वापस नहीं देता, library books कभी return न करने की तरह। समय के साथ computer का space खत्म हो जाता है।
Syntax
cpp
data_type *pointer_name = new data_type(value);
// missing delete causes a leak
delete pointer_name;

Memory Leak क्या है?

एक memory leak तब होती है जब new से allocated heap memory कभी एक matching delete के साथ release नहीं होती, इसलिए program इसे free करने का कोई तरीका खो देता है भले ही यह अब इस्तेमाल न हो रही हो।

leaks वाला एक लंबे समय तक चलने वाला program धीरे-धीरे ज़्यादा से ज़्यादा RAM consume करता है जब तक performance degrade न हो या system memory खत्म न हो जाए।

उदाहरण: What is a Memory Leak?

cpp
#include <iostream>

int main() {
	int *ptr = new int(5);
	// missing delete ptr; here would leak this memory
	delete ptr;
	std::cout << "Freed correctly" << std::endl;
	return 0;
}

बिना Deallocate किए Pointers Reassign करना

अगर आप पहले old address पर delete call किए बिना एक pointer को एक नए address पर reassign करते हैं, original memory block unreachable हो जाता है -- program में कहीं भी कोई remaining pointer नहीं है जो इसे reference करता हो, इसलिए यह कभी free नहीं हो सकता, भले ही OS के हिसाब से यह technically अभी भी "in use" हो।

उदाहरण: Reassigning Pointers without Deallocating

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	int *ptr = new int(5);
	// Free the heap memory with delete
	delete ptr;
	// Allocate an object on the heap with new
	ptr = new int(10);
	// Print to the console with cout
	std::cout << *ptr << std::endl;
	// Free the heap memory with delete
	delete ptr;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Functions के अंदर Leaks

एक function के अंदर new से memory allocate करना और address सिर्फ एक local pointer variable में store करना मतलब वह address function return होते ही खो जाता है, जब तक आप explicitly pointer return न करें या इसे कहीं ऐसी जगह store न करें जो function call से outlive करे।

नहीं तो वह memory हर call पर silently leak होती है।

उदाहरण: Leaks inside Functions

cpp
// Include std::cout and std::cin
#include <iostream>

int *createValue() {
	// Allocate an object on the heap with new
	int *ptr = new int(42);
	// Send ptr back to the caller
	return ptr;
}

// Program execution starts in main()
int main() {
	int *result = createValue();
	// Print to the console with cout
	std::cout << *result << std::endl;
	// Free the heap memory with delete
	delete result;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dynamic Arrays और Incorrect Delete

एक dynamically allocated array को delete[] की बजाय plain delete से free करना सिर्फ पहले element को सही तरीके से destruct और free करता है -- array की बाकी memory कभी properly release नहीं होती, एक partial leak cause करते हुए जो miss करना आसान है क्योंकि program तुरंत crash नहीं होता।

उदाहरण: Dynamic Arrays and Incorrect Delete

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	int *arr = new int[5];
	// Free the heap memory with delete
	delete[] arr;
	// Print to the console with cout
	std::cout << "Array freed correctly" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Prevention के लिए Best Practices

leaks से बचने का सबसे reliable तरीका यह minimize करना है कि आप raw new/delete pairs कितनी देर hold करते हैं, और C++ smart pointers (unique_ptr, shared_ptr) prefer करना जो scope से बाहर जाने पर automatically delete call करते हैं -- manual bookkeeping को कुछ ऐसा बना देते हुए जो compiler आपके लिए enforce करता है।

उदाहरण: Best Practices for Prevention

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 = std::make_unique<int>(5);
	// 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. old वाले को delete किए बिना pointer को एक नए allocation पर reassign करना, जो only reference खो देता है।
  2. delete तक पहुँचने से पहले एक function से early return करना।
  3. new[] से allocated pointer पर 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.