-->

Sunday, March 1, 2015

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

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

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.

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved