In this post, we will understand the difference between malloc and calloc.
The method ‘malloc’ is used to assign a block of memory when it is requested.
It doesn’t clear the memory.
It only initializes the allocated memory when explicitly requested.
It allocates memory of a specific ‘size’.
This size is passed as parameter to it.
This size is allocated from a heap.
It performs its job quickly.
void *malloc(size_t size);
It assigns the requested memory to multiple blocks.
This memory allocated is initiated to zero.
This initialization to 0 is done by ‘calloc’ method.
It allocates memory to the required operation of a specific ‘size’, i.e num * size.
The ‘num’ refers to number of blocks of memory.
It is slow in comparison to ‘malloc’ method.
void *calloc(size_t num, size_t size);