C का malloc()
In this page:
#include <stdlib.h>
data_type *pointer_name = (data_type *) malloc(count * sizeof(data_type));
if (pointer_name == NULL) {
// allocation failed
}
Dynamic Memory Allocation
यह stack memory से अलग है, जो function के return होने पर automatically sized और reclaim होती है -- heap memory तब तक रहती है जब तक आप इसे explicitly free न करें, और इसका size program के actually चलते हुए, real input के आधार पर decide किया जा सकता है।
उदाहरण: Dynamic Memory 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() {
// 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
malloc() Function
क्योंकि malloc() एक generic void pointer return करता है, आप आमतौर पर इसे चाहिए होने वाले type में cast करते हैं, जैसे int *arr = (int*)malloc(10 * sizeof(int)) -- ध्यान दें memory uninitialized होती है, जिसमें पहले जो भी garbage bytes थे वे होते हैं।
उदाहरण: The malloc() Function
// 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 *arr = (int*)malloc(10 * sizeof(int));
arr[0] = 1;
// Print formatted text to the screen
printf("%d", arr[0]);
// Release the heap memory so it can be reused
free(arr);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Allocation Success Check करना
production code में यह check optional नहीं है -- एक failed malloc() जो NULL return करता है उसे dereference करना आपके program को तुरंत crash कर देता है, और limited memory वाले systems पर, allocation failures एक real possibility हैं, सिर्फ एक theoretical edge case नहीं।
उदाहरण: Checking Allocation Success
// 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));
// Check whether ptr == NULL
if (ptr == NULL) {
// Print formatted text to the screen
printf("Allocation failed");
// Send 1 back to the caller
return 1;
}
*ptr = 5;
// Print formatted text to the screen
printf("%d", *ptr);
free(ptr);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
free() से Memory Free करना
यह step skip करना एक memory leak cause करता है: allocated block इसका pointer खो देते ही unreachable हो जाता है (जैसे pointer variable scope से बाहर चला जाए), लेकिन operating system program के exit होने तक उस memory को अभी भी 'in use' मानता है।
उदाहरण: Freeing Memory with 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;
// 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
malloc() से Dynamic Arrays
Multiplication (count * sizeof(type)) यह पक्का करता है कि आप target platform के specific type sizes की परवाह किए बिना exactly सही संख्या में bytes request करें, जो systems के बीच अलग हो सकते हैं -- एक byte count hardcode करने की बजाय हमेशा sizeof इस्तेमाल करें।
उदाहरण: Dynamic Arrays with malloc()
// 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 count and set it to 5
int count = 5;
// Allocate memory on the heap
int *arr = malloc(count * sizeof(int));
// Loop: repeat while the condition holds
for (int i = 0; i < count; i++) {
arr[i] = i;
}
// Print formatted text to the screen
printf("%d", arr[4]);
// Release the heap memory so it can be reused
free(arr);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- memory इस्तेमाल करने से पहले यह check न करना कि
mallocनेNULLreturn किया या नहीं। freecall करना भूल जाना, ताकि memory program खत्म होने तक leak होती रहे।n * sizeof(type)की बजायnelements चाहिए होने परnbytes allocate करना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: