In c language article we will see the difference between malloc() and calloc(). Both are the functions in c language. See the table which is mentioned below:
Malloc()
|
Calloc()
|
The Syntax of malloc() is :
Ptr = (data_type *) malloc(size);
|
The required number of bytes to be allocated is specified as argument
i.e. size un bytes.
The Syntax of calloc() is :
Ptr = (data_type*)calloc(n,size);
Takes two arguments: n is number of blocks to be allocated, size is
number of bytes to be allocated for each block.
Allocates a block of memory of size bytes.
Allocates multiple blocks of memory, each block with the same size.
Allocated space will not be initialized
Each byte of allocated space is initialized to zero.
Since no initialization takes place, time efficiency is higher than
calloc()
Calloc() is slightly more computationally expensive because of zero
filling but, occasionally, more convenient than malloc().
This function can allocate the required size of memory even if the
memory is not available contiguously but available at different locations
This function can allocate the required number of blocks
contiguously. If required memory cannot be allocated contiguously, it returns
NULL.