C++ में Memory Leaks
In this page:
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?
#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;
}
Login to try C/C++/Java/PHP code in the editor
बिना 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- old वाले को delete किए बिना pointer को एक नए allocation पर reassign करना, जो only reference खो देता है।
deleteतक पहुँचने से पहले एक function से early return करना।new[]से allocated pointer परdeleteइस्तेमाल करना।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: