calloc function in C language with an example program
Introduction:
We have looked at the static vs dynamic memory allocation and malloc() function in earlier articles, In today’s article, We are going to look at the calloc function in c programming language
Must Read:
Calloc function in C Language:
The calloc() function is used to allocate multiple chunks of memory dynamically. Here is the syntax of the calloc() function in c language.
Syntax of callo() function in c:
1 |
void *calloc(size_t n_chuncks, size_t size); |
Here
- n_chunks is the number of chunks or blocks
- size is the size of each chunk.
So the calloc() function allocates the n_chunks and the size of each chunk is size
Let’s look at an example
Example use of Calloc() function in c programming:
1 2 3 |
int p; p = (int *) calloc (10, sizeof(int) ); |
We are allocating the 10 blocks of memory and each block of 4 Bytes size. (Assuming integer size is 4 Bytes). So in total, we are allocating the 40 Bytes, that can accommodate for the storage of ten integer variables.
Similar to the malloc(), the calloc() function also returns NULL if it is failed to allocate the memory. So please check the pointer value before using it.
Program to Allocate memory dynamically using the calloc() function in c programming language:
Here is a program to dynamically allocate memory using calloc() function in c language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
/* Program to allocate memory using calloc() function sillycodes.com */ #include <stdio.h> #include <stdlib.h> int main() { int nChunks, i; printf("How many integers you want to store: "); scanf("%d", &nChunks); int *ptr; // call calloc() function ptr = (int *) calloc(nChunks, sizeof(int)); // check pointer if(ptr == NULL) { printf("ERROR! Failed to Allocate Memory\n"); return 0; } printf("SUCCESS! Memory allocated successfully using calloc() function\n"); // Take the use input and update the integers for(i = 0; i<nChunks; i++) { // use scanf to store the data printf("Enter a Number: "); scanf("%d", ptr+i); } // print the integers printf("Entered Integers are : "); for(i = 0; i<nChunks; i++) { printf("%d ", *(ptr+i) ); // we can also use ptr[i] } // new line printf("\n"); // free the memory. free(ptr); return 0; } |
The program prompts the user to provide the number of blocks or chunks and stores in the nChunks variable. Then it will use the calloc() function to create the nChunks memory dynamically, where each chunk is equal to the size of the integer variable.
1 |
ptr = (int *) calloc(nChunks, sizeof(int)); |
Once the memory is allocated, We update the memory blocks with the user-provided data. We use a for loop to traverse through the memory.
1 2 3 4 5 6 7 |
// Take the use input and update the integers for(i = 0; i<nChunks; i++) { // use scanf to store the data printf("Enter a Number: "); scanf("%d", ptr+i); } |
Finally, We display the provided numbers on the console using another for loop.
1 2 3 4 5 |
printf("Entered Integers are : "); for(i = 0; i<nChunks; i++) { printf("%d ", *(ptr+i) ); // we can also use ptr[i] } |
Program Output:
Let’s compile the program using your favorite compiler. Here we are using the GCC compiler.
$ gcc calloc.c
Run the executable file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ ./a.out How many integers you want to store: 10 SUCCESS! Memory allocated successfully using calloc() function Enter a Number: 7 Enter a Number: 9 Enter a Number: 2 Enter a Number: 9 Enter a Number: 0 Enter a Number: 1 Enter a Number: 9 Enter a Number: 4 Enter a Number: 9 Enter a Number: 5 Entered Integers are : 7 9 2 9 0 1 9 4 9 5 $ |
As we can see from the above output, We are able to allocate the memory for 10 Integer variables using the calloc() function, Then we updated the allocated memory with user-provided data.
Let’s try one more example.
1 2 3 4 5 6 7 8 9 |
$ ./a.out How many integers you want to store: 4 SUCCESS! Memory allocated successfully using calloc() function Enter a Number: 88 Enter a Number: 44 Enter a Number: 63 Enter a Number: 77 Entered Integers are : 88 44 63 77 $ |
The program is working as expected and allocating the requested amount of memory dynamically using calloc() function.
What are the differences between the malloc and calloc function in C:
Firstly, we can see the syntactical difference. The malloc() function takes only one argument, while the calloc() function takes two arguments.
malloc() function syntax:
1 |
ptr = (int *) malloc(size); |
calloc() function syntax:
1 |
ptr = (int *) calloc(nChunks, size); |
One more difference is, The memory blocks allocated by the calloc() function are initialized with zeros. While the malloc() allocated memory contains the garbage values.
Related Articles:
- C Tutorials Index
- C Programs Index – 300+ Programs
- Size of Pointer in C Language
- Add Two Numbers using Pointers in C Language
- Program to perform Arithmetic Operations using Pointers in C
- Swap Two Numbers using Pointers in C Language
- Pointer Arithmetic in C Language
- Pointer to Pointer in C – Double Pointer in C
- Pointers and Arrays in C Language with Example Programs
- Accessing Array Elements using Pointers in C
- Print String Elements using Pointers in C Language
- Function Returning Pointer in C Programming Language
- Function Pointers in C with Example Programs