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

C में Memory Leaks

एक memory leak तब होता है जब एक program memory borrow करता है और कभी वापस नहीं देता, library books कभी वापस न करने जैसा। समय के साथ computer की space खत्म हो जाती है।
Syntax
c
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?

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

Memory Leaks के Common Causes

एक खासकर sneaky cause है एक pointer variable को इसके पहले point की गई चीज़ को free किए बिना एक नए malloc() result में reassign करना -- pointer overwrite होते ही पुराना block बिना किसी error या warning के unreachable हो जाता है।

उदाहरण: Common causes of memory leaks

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

Memory Leaks Detect करना

Valgrind जैसे dedicated tools आपका program चला सकते हैं और exactly report कर सकते हैं कि कौन सी allocations कभी free नहीं हुईं, साथ में उन्हें allocate करने वाली code की line -- manual tracing छोटे programs के लिए काम करता है लेकिन बड़े codebases में अच्छे से scale नहीं करता।

उदाहरण: Detecting memory leaks

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("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;
}

Loops में Memory Leaks Fix करना

एक loop जो हर iteration पर malloc() call करता है लेकिन सिर्फ loop के बाहर free करता है (या बिल्कुल नहीं) surprisingly जल्दी available memory खत्म कर सकता है, क्योंकि हर iteration पिछले के ऊपर एक और leaked block add करता है।

उदाहरण: Fixing memory leaks in loops

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() {
	// 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;
}

Memory Management Strategies

बड़े codebases में, यह document करना आम है (एक comment में या function के नाम में ही) कि क्या एक function returned memory का ownership caller को transfer करता है, क्योंकि कुछ free करने की responsibility किसकी है इसमें ambiguity leaks का एक leading cause है।

उदाहरण: Memory management strategies

c
// 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;
}
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. पुराने को free किए बिना एक pointer को एक नई allocation पर reassign करना, जो इकलौता reference खो देता है।
  2. free call तक पहुँचने से पहले एक function से जल्दी return करना।
  3. एक 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:

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.