C का free()
In this page:
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?
// 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
free() कैसे इस्तेमाल करें
free() pointer variable को खुद erase नहीं करता, सिर्फ इसकी point की गई memory को -- इसलिए pointer बाद में भी same (अब-invalid) address रखता है, यही वजह है कि free करने के तुरंत बाद इसे NULL set करना अच्छी practice मानी जाती है।
उदाहरण: How to use free()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Double free Error
यह corruption इसलिए होता है क्योंकि पहली free() call के बाद free() अपनी bookkeeping के लिए internally same memory region reuse कर सकता है, इसलिए same address पर एक दूसरा free() allocator के internal data structures corrupt कर सकता है।
उदाहरण: Double free error
// 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;
}
Login to try C/C++/Java/PHP code in the editor
free() के बाद Dangling Pointers
यह C bugs की सबसे कुख्यात classes में से एक है (use-after-free), क्योंकि pointer बिल्कुल valid दिखता है और इसे dereference करना coincidence से काम भी कर सकता है, जब तक यह चुपचाप unrelated data corrupt न कर दे।
उदाहरण: Dangling pointers after free()
// 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
Dynamic Structures Free करना
यह ordering मायने रखता है क्योंकि एक बार आप outer structure free कर दें, इसके अंदर के inner pointers तक पहुँचने का कोई तरीका नहीं बचता -- freeing हमेशा 'inside out' होनी चाहिए, सबसे deeply nested allocations से पहले शुरू करते हुए।
उदाहरण: Freeing dynamic structures
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- same pointer पर
freeको दो बार call करना, जो undefined behavior है। - एक pointer को free करने के बाद इस्तेमाल करना (dangling pointer)।
- उस memory पर
freecall करना जोmallocसे allocate नहीं हुई थी।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: