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

C++ में Memory Management

C++ में memory management, new से heap पर manually memory allocate करने और delete से इसे release करने, या उस lifecycle को safely automate करने के लिए RAII-based smart pointers इस्तेमाल करने का process है।
Syntax
cpp
data_type stack_variable = value;  // stack: freed automatically

data_type *heap_pointer = new data_type(value);  // heap: you free it
delete heap_pointer;

Memory Management क्या है?

C++ में memory management data के लिए space allocate करने का process है जब program को इसकी ज़रूरत हो और उस space को release करने का जब इसकी ज़रूरत न रहे, new/delete से manually या RAII-based smart pointers से automatically किया जाता है।

उदाहरण: What is Memory Management?

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

Stack बनाम Heap Memory

C++ programs memory के दो main regions इस्तेमाल करते हैं: stack, जो function calls से जुड़े local variables automatically manage करता है, और heap, memory का एक pool जिसे explicitly allocate और free किया जाना चाहिए (या एक smart pointer से manage किया जाना चाहिए)।

उदाहरण: Stack vs Heap Memory

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

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

Static बनाम Dynamic Allocation

Static allocation, एक regular array declaration की तरह, compile time पर memory size fix करती है, जबकि dynamic allocation, new इस्तेमाल करते हुए, एक program को actually run होते वक्त decide करने देती है कि इसे कितनी memory चाहिए।

उदाहरण: Static vs Dynamic Allocation

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

// Program execution starts in main()
int main() {
	int staticArr[3] = {1, 2, 3};
	// Declare size and set it to 3
	int size = 3;
	// Allocate an object on the heap with new
	int *dynamicArr = new int[size];
	dynamicArr[0] = 5;
	// Print to the console with cout
	std::cout << staticArr[0] << " " << dynamicArr[0] << std::endl;
	// Free the heap memory with delete
	delete[] dynamicArr;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Allocate-Use-Free Lifecycle

dynamically allocated memory का हर block एक lifecycle follow करता है: यह new से allocate होता है, एक pointer के जरिए इस्तेमाल होता है, और फिर जब इसकी ज़रूरत न रहे exactly एक बार delete (या arrays के लिए delete[]) से release होता है।

उदाहरण: The Allocate-Use-Free Lifecycle

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

Modern C++ Smart Pointers Prefer करता है

Modern C++ raw new/delete की बजाय unique_ptr और shared_ptr जैसे smart pointers इस्तेमाल करने को encourage करता है, क्योंकि ये scope से बाहर जाने पर automatically अपनी memory release करते हैं, leaks और use-after-free bugs को रोकते हुए।

उदाहरण: Modern C++ Prefers Smart Pointers

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>(42);
	// 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. new से allocated memory free करना भूल जाना, जो एक leak cause करता है।
  2. free होने के बाद memory इस्तेमाल करना, जो undefined behavior है।
  3. एक function से एक local (stack) variable का address return करना।
🔒

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.