The name malloc and calloc() are library functions that allocate memory dynamically. It means that memory is allocated during runtime(execution of the program) from heap segment.
- Initialization: malloc() allocates memory block of given size (in bytes) and returns a pointer to the beginning of the block. malloc() doesn’t initialize the allocated memory. If we try to acess the content of memory block then we’ll get garbage values.
void*malloc(size_tsize );chevron_rightfilter_nonecalloc() allocates the memory and also initializes the allocates memory block to zero. If we try to access the content of these blocks then we’ll get 0.
void*calloc(size_tnum,size_tsize );chevron_rightfilter_none - Number of arguments: Unlike malloc(), calloc() takes two arguments:
1) Number of blocks to be allocated.
2) Size of each block. - Return Value: After successfull allocation in malloc() and calloc(), a pointer to the block of memory is returned otherwise NULL value is returned which indicates the failure of allocation.
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- For Versus While
- typedef versus #define in C
- Scope Resolution Operator Versus this pointer in C++?
- malloc() vs new
- Function Interposition in C with an example of user defined malloc()
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- C program to store Student records as Structures and Sort them by Name
- Similarities and Differences between Ruby and C language
- Program to copy the contents of one array into another in the reverse order
- Interesting facts about C Language
- Program to Reverse a String using Pointers
- Difference between Structure and Array in C
- Structured Programming Approach with Advantages and Disadvantages
For instance, If we want to allocate memory for array of 5 integers, see the following program:-
// C program to demonstrate the use of calloc() // and malloc() #include <stdio.h> #include <stdlib.h> int main() { int *arr; // malloc() allocate the memory for 5 integers // containing garbage values arr = (int *)malloc(5 * sizeof(int)); // 5*4bytes = 20 bytes // Deallocates memory previously allocated by malloc() function free( arr ); // calloc() allocate the memory for 5 integers and // set 0 to all of them arr = (int *)calloc(5, sizeof(int)); // Deallocates memory previously allocated by calloc() function free(arr); return(0); } |
We can achieve same functionality as calloc() by using malloc() followed by memset(),
ptr = malloc(size); memset(ptr, 0, size); |
Note: It would be better to use malloc over calloc, unless we want the zero-initialization because malloc is faster than calloc. So if we just want to copy some stuff or do something that doesn’t require filling of the blocks with zeros, then malloc would be a better choice.
This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Recommended Posts:
Improved By : chakradharkandula



