← Back to C Course | Chapter 9: Memory Management | Lesson 1 of 8

C में Memory Management

C में memory management ज़रूरत पड़ने पर manually memory allocate करने और ज़रूरत न रहने पर इसे release करने का process है, leaks और undefined behavior से बचने के लिए एक allocate-use-free lifecycle follow करते हुए।
Syntax
c
#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?

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

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

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

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

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

Allocate-Use-Free Lifecycle

Dynamically allocated memory का हर block एक lifecycle follow करता है: यह malloc जैसे function से allocate होता है, एक pointer से इस्तेमाल होता है, और फिर ज़रूरत न रहने पर exactly एक बार free से release होता है।

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

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

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

c
// 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;
}
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. malloc से मिली memory को free करना भूल जाना, जो एक memory leak cause करता है।
  2. free होने के बाद memory इस्तेमाल करना, जो undefined behavior है।
  3. एक local variable का address return करना, जो stack पर रहता है और return पर destroy हो जाता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.