C का calloc()
In this page:
data_type *pointer_name = (data_type *) calloc(count, sizeof(data_type)); // all bytes set to zero
calloc() क्या है?
zero-initialization की एक real cost है -- calloc() को पूरे block में zeros लिखने पड़ते हैं, जो malloc() के मुकाबले थोड़ा extra काम है, लेकिन एक known state से शुरू करने की safety guarantee के लिए यह आमतौर पर worth है।
उदाहरण: What is calloc()?
// 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 *arr = calloc(3, sizeof(int));
// Print formatted text to the screen
printf("%d %d %d", arr[0], arr[1], arr[2]);
// 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
calloc() बनाम malloc()
Practice में, calloc(n, size) roughly इसे zero out करने के लिए memset() के बाद malloc(n * size) के बराबर है, लेकिन calloc() इस multiplication में integer overflow से भी protect करता है जिस तरह manual code अक्सर नहीं करता।
उदाहरण: calloc() vs 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() {
// Allocate memory on the heap
int *a = malloc(3 * sizeof(int));
int *b = calloc(3, sizeof(int));
// Print formatted text to the screen
printf("%d", b[0]);
// Release the heap memory so it can be reused
free(a);
// Release the heap memory so it can be reused
free(b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Arrays Allocate करना
यह convenience खासकर counters या accumulators के arrays के लिए valuable है, जहाँ zero से शुरू करना exactly वह behavior है जो आप चाहते हैं, allocate करने के बाद एक loop में इसे manually setup करने की बजाय।
उदाहरण: Allocating Arrays
// 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 *counters = calloc(5, sizeof(int));
counters[2]++;
// Print formatted text to the screen
printf("%d", counters[2]);
// Release the heap memory so it can be reused
free(counters);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Structures Allocate करना
pointer members वाले एक struct के लिए, zero-initialization का मतलब है वे pointers garbage addresses की बजाय NULL से शुरू होते हैं, जो उन fields के properly assign होने से पहले बाद की NULL-checks को meaningful बनाता है।
उदाहरण: Allocating 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() {
struct Node *n = calloc(1, sizeof(struct Node));
// Print formatted text to the screen
printf("%d", n->next == NULL);
// Release the heap memory so it can be reused
free(n);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
calloc() के साथ Common Mistakes
malloc() की तरह ही, हमेशा calloc() की return value को पहले एक temporary variable को assign करें और अपने original pointer को overwrite करने से पहले इसे check करें, ताकि अगर यह call fail हो तो आप पहले से allocated memory का अपना इकलौता reference न खोएं।
उदाहरण: Common Mistakes with calloc()
// 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 *ptr = calloc(3, sizeof(int));
int *temp = calloc(3, sizeof(int));
// Check whether temp != NULL
if (temp != NULL) {
// Release the heap memory so it can be reused
free(ptr);
ptr = temp;
}
// Print formatted text to the screen
printf("%d", ptr[0]);
// 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
- यह check न करना कि
callocनेNULLreturn किया। - यह मान लेना कि
mallocभी memory zero करता है, जब सिर्फcallocकरता है। callocसे allocated memory कोfreeकरना भूल जाना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: