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

C का calloc()

calloc भी program चलते समय memory मांगता है, लेकिन यह पहले सब कुछ zero set कर देता है, एक ताज़ा wiped whiteboard देने जैसा। यह arrays के लिए handy है।
Syntax
c
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()?

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

calloc() बनाम malloc()

Practice में, calloc(n, size) roughly इसे zero out करने के लिए memset() के बाद malloc(n * size) के बराबर है, लेकिन calloc() इस multiplication में integer overflow से भी protect करता है जिस तरह manual code अक्सर नहीं करता।

उदाहरण: calloc() vs malloc()

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

Arrays Allocate करना

यह convenience खासकर counters या accumulators के arrays के लिए valuable है, जहाँ zero से शुरू करना exactly वह behavior है जो आप चाहते हैं, allocate करने के बाद एक loop में इसे manually setup करने की बजाय।

उदाहरण: Allocating Arrays

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

Structures Allocate करना

pointer members वाले एक struct के लिए, zero-initialization का मतलब है वे pointers garbage addresses की बजाय NULL से शुरू होते हैं, जो उन fields के properly assign होने से पहले बाद की NULL-checks को meaningful बनाता है।

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

calloc() के साथ Common Mistakes

malloc() की तरह ही, हमेशा calloc() की return value को पहले एक temporary variable को assign करें और अपने original pointer को overwrite करने से पहले इसे check करें, ताकि अगर यह call fail हो तो आप पहले से allocated memory का अपना इकलौता reference न खोएं।

उदाहरण: Common Mistakes with calloc()

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() {
	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;
}
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. यह check न करना कि calloc ने NULL return किया।
  2. यह मान लेना कि malloc भी memory zero करता है, जब सिर्फ calloc करता है।
  3. calloc से allocated memory को free करना भूल जाना।
🔒

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.