C++ में Memory Management
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Allocate-Use-Free Lifecycle
dynamically allocated memory का हर block एक lifecycle follow करता है: यह new से allocate होता है, एक pointer के जरिए इस्तेमाल होता है, और फिर जब इसकी ज़रूरत न रहे exactly एक बार delete (या arrays के लिए delete[]) से release होता है।
उदाहरण: The Allocate-Use-Free Lifecycle
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
newसे allocated memory free करना भूल जाना, जो एक leak cause करता है।- free होने के बाद memory इस्तेमाल करना, जो undefined behavior है।
- एक function से एक local (stack) variable का address return करना।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: