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

C का free()

free borrowed memory computer को वापस दे देता है, एक library book वापस करने जैसा। इसे दो बार या return करने के बाद इस्तेमाल करना problems cause करता है।
Syntax
c
free(pointer_name);
pointer_name = NULL;  // avoid a dangling pointer

free() Memory क्यों करें?

यह manual responsibility C के memory model का core tradeoff है -- unused memory देखने वाला कोई garbage collector नहीं है, इसलिए हर successful malloc(), calloc(), या realloc() call को आपके program की logic में कहीं एक matching free() चाहिए।

उदाहरण: Why free() 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() {
	// 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;
}

free() कैसे इस्तेमाल करें

free() pointer variable को खुद erase नहीं करता, सिर्फ इसकी point की गई memory को -- इसलिए pointer बाद में भी same (अब-invalid) address रखता है, यही वजह है कि free करने के तुरंत बाद इसे NULL set करना अच्छी practice मानी जाती है।

उदाहरण: How to use free()

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 = 10;
	// 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;
}

Double free Error

यह corruption इसलिए होता है क्योंकि पहली free() call के बाद free() अपनी bookkeeping के लिए internally same memory region reuse कर सकता है, इसलिए same address पर एक दूसरा free() allocator के internal data structures corrupt कर सकता है।

उदाहरण: Double free error

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);
	ptr = NULL;
	// Print formatted text to the screen
	printf("%d", ptr == NULL);
	// Return 0 to signal that the program finished successfully
	return 0;
}

free() के बाद Dangling Pointers

यह C bugs की सबसे कुख्यात classes में से एक है (use-after-free), क्योंकि pointer बिल्कुल valid दिखता है और इसे dereference करना coincidence से काम भी कर सकता है, जब तक यह चुपचाप unrelated data corrupt न कर दे।

उदाहरण: Dangling pointers after free()

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

Dynamic Structures Free करना

यह ordering मायने रखता है क्योंकि एक बार आप outer structure free कर दें, इसके अंदर के inner pointers तक पहुँचने का कोई तरीका नहीं बचता -- freeing हमेशा 'inside out' होनी चाहिए, सबसे deeply nested allocations से पहले शुरू करते हुए।

उदाहरण: Freeing dynamic structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Define a structure type named Node
struct Node { int value; struct Node *next; };
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	struct Node *inner = malloc(sizeof(struct Node));
	// Allocate memory on the heap
	struct Node *outer = malloc(sizeof(struct Node));
	outer->next = inner;
	// Release the heap memory so it can be reused
	free(inner);
	// Release the heap memory so it can be reused
	free(outer);
	printf("Freed inside-out");
	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. same pointer पर free को दो बार call करना, जो undefined behavior है।
  2. एक pointer को free करने के बाद इस्तेमाल करना (dangling pointer)।
  3. उस memory पर free call करना जो malloc से allocate नहीं हुई थी।
🔒

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.