C में Memory Management
In this page:
#include <stdlib.h>
data_type *pointer_name = malloc(size_in_bytes);
free(pointer_name);
Memory Management क्या है?
C में memory management program को जब data चाहिए तो उसके लिए space allocate करने और ज़रूरत न रहने पर उस space को release करने का process है, एक responsibility जो programmer manually handle करता है, न कि कोई automatic garbage collector।
उदाहरण: What is Memory Management?
// 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
Stack बनाम Heap Memory
C programs memory के दो main regions इस्तेमाल करते हैं: stack, जो function calls से जुड़े local variables automatically manage करता है, और heap, memory का एक pool जिसे programmer को explicitly allocate और free करना होता है।
उदाहरण: Stack vs Heap Memory
// 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() {
// Declare stackVar and set it to 5
int stackVar = 5;
// Allocate memory on the heap
int *heapVar = malloc(sizeof(int));
*heapVar = 10;
// Print formatted text to the screen
printf("%d %d", stackVar, *heapVar);
// Release the heap memory so it can be reused
free(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, malloc जैसे functions इस्तेमाल करके, एक program को actually चलते समय decide करने देता है कि इसे कितनी memory चाहिए।
उदाहरण: Static vs Dynamic Allocation
// 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() {
int staticArr[5];
// Allocate memory on the heap
int *dynamicArr = malloc(5 * sizeof(int));
// Print formatted text to the screen
printf("%zu %d", sizeof(staticArr), dynamicArr != NULL);
// Release the heap memory so it can be reused
free(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 करता है: यह malloc जैसे function से allocate होता है, एक pointer से इस्तेमाल होता है, और फिर ज़रूरत न रहने पर exactly एक बार free से release होता है।
उदाहरण: The Allocate-Use-Free Lifecycle
// 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 = 42;
// 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
Common Memory Management Mistakes
सबसे common memory management mistakes memory leaks हैं, जहाँ allocated memory कभी free नहीं होती, और use-after-free, जहाँ एक pointer को उसकी memory release होने के बाद dereference किया जाता है, दोनों serious bugs की ओर ले जाते हैं।
उदाहरण: Common Memory Management Mistakes
// 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;
// Release the heap memory so it can be reused
free(ptr);
ptr = NULL;
// Print formatted text to the screen
printf("%d", ptr == NULL);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
mallocसे मिली memory कोfreeकरना भूल जाना, जो एक memory leak cause करता है।- free होने के बाद memory इस्तेमाल करना, जो undefined behavior है।
- एक local variable का address return करना, जो stack पर रहता है और return पर destroy हो जाता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: