Difference between malloc( ) and calloc() in C language

March 01, 2015 , 0 Comments

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:

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.

Bill Smithwick

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google