Difference Between malloc and calloc


In this post, we will understand the difference between malloc and calloc.

Malloc

  • 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.

Example

void *malloc(size_t size);

Calloc

  • 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.

Example

void *calloc(size_t num, size_t size);

Updated on: 24-Mar-2021

721 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements