C में Memory Leaks
In this page:
type *ptr = malloc(sizeof(type)); // allocate on the heap
// ... use ptr ...
free(ptr); // release it, or the memory leaks
Memory Leak क्या है?
एक crash के उलट, एक leak तुरंत visible failure cause नहीं करता -- program चलता रहता है, लेकिन इसका memory footprint समय के साथ बढ़ता है, जो servers जैसे long-running programs में खासकर dangerous है जो हफ्तों या महीनों चल सकते हैं।
उदाहरण: What is a memory leak?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = 5;
// Print formatted text to the screen
printf("%d", *ptr);
// Release the heap memory so it can be reused
free(ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Leaks के Common Causes
एक खासकर sneaky cause है एक pointer variable को इसके पहले point की गई चीज़ को free किए बिना एक नए malloc() result में reassign करना -- pointer overwrite होते ही पुराना block बिना किसी error या warning के unreachable हो जाता है।
उदाहरण: Common causes of memory leaks
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
// Release the heap memory so it can be reused
free(ptr);
// Allocate memory on the heap
ptr = malloc(sizeof(int));
*ptr = 5;
// Print formatted text to the screen
printf("%d", *ptr);
// Release the heap memory so it can be reused
free(ptr);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Leaks Detect करना
Valgrind जैसे dedicated tools आपका program चला सकते हैं और exactly report कर सकते हैं कि कौन सी allocations कभी free नहीं हुईं, साथ में उन्हें allocate करने वाली code की line -- manual tracing छोटे programs के लिए काम करता है लेकिन बड़े codebases में अच्छे से scale नहीं करता।
उदाहरण: Detecting memory leaks
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = 5;
// Print formatted text to the screen
printf("Run with Valgrind to detect unfreed allocations");
// Release the heap memory so it can be reused
free(ptr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Loops में Memory Leaks Fix करना
एक loop जो हर iteration पर malloc() call करता है लेकिन सिर्फ loop के बाहर free करता है (या बिल्कुल नहीं) surprisingly जल्दी available memory खत्म कर सकता है, क्योंकि हर iteration पिछले के ऊपर एक और leaked block add करता है।
उदाहरण: Fixing memory leaks in loops
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = i;
// Release the heap memory so it can be reused
free(ptr);
}
// Print formatted text to the screen
printf("Freed each iteration's allocation");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Management Strategies
बड़े codebases में, यह document करना आम है (एक comment में या function के नाम में ही) कि क्या एक function returned memory का ownership caller को transfer करता है, क्योंकि कुछ free करने की responsibility किसकी है इसमें ambiguity leaks का एक leading cause है।
उदाहरण: Memory management strategies
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
int* createValue() {
// Allocate memory on the heap
int *ptr = malloc(sizeof(int));
*ptr = 5;
// Send ptr back to the caller
return ptr;
}
// Program execution starts in main()
int main() {
int *value = createValue();
// Print formatted text to the screen
printf("%d", *value);
// Release the heap memory so it can be reused
free(value);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- पुराने को free किए बिना एक pointer को एक नई allocation पर reassign करना, जो इकलौता reference खो देता है।
freecall तक पहुँचने से पहले एक function से जल्दी return करना।- एक loop के अंदर memory allocate करना बिना हर बार इसे free किए।
- Dynamic memory run time पर
malloc()औरcalloc()से request की जाती है, औरrealloc()से resize की जाती है। - जिस memory की अब ज़रूरत नहीं उसे
free()से release करना ज़रूरी है, वरना यह leak करती है। - same allocation approach structs के लिए काम करता है, और allocated memory access करते समय careful रहना ज़रूरी है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: