Difference between Calloc and Malloc

Key difference: ‘Calloc’ and ‘Malloc’ refers to the performance of dynamic memory allocation in the C programming language. A ‘calloc’ initializes the allocated memory with zero, whereas a ‘malloc’ does not.

Both ‘calloc’ and ‘malloc’ are standard library functions.

A ‘calloc’ is a group of C programming standard library function. It basically allocates the dynamic memory in the C language. It provides storage to a variable in a running program. The library function is written as calloc (num, size). This function takes two arguments that specify the number of elements to be reserved, and the size of each element in bytes. It allocates the memory block equivalent to the num * size. It also returns a pointer to the beginning of the allocated storage area in the memory. The ‘calloc’ function is used to reserve the space for dynamic arrays.

Its syntax is as follows:

void *calloc (number_of_blocks, size_of_each_block_in_bytes);

Here, ‘void’ is a general C function, which does not return any value. The number of elements in the first argument specifies the size in bytes of one element to the second argument. The address is returned after the successful partitioning, otherwise a NULL is returned on any failure of the function.

A ‘malloc’ is a memory allocation function in the C language. It manually allocates memory to some of the C data structures. It allocates a section of the memory of a particular size for those data structures. The malloc() function allocates memory from a heap. Automatic variables that are declared inside the function are placed on the heap. It only allocates the memory of the required number of bytes. The dynamic memory allocated in the ‘malloc’ function can be used anywhere in the program by knowing the address.

Its syntax is as follows:

void *malloc (size_in_bytes);

Specifically, it returns the address in a generic manner (void *). If ‘malloc’ fails to allocate the memory, it returns -1, otherwise it returns the address from which the memory is allocated. The allocated memory can be in the form of an integer, by type casting the void address as int *; the allocated memory can also be in the form of a float, just by type casting the same void address as float *.

Comparison between Calloc and Malloc:




Basic functions

The library function allocates a region of the memory, which is large enough to hold the “n elements” of “size” bytes each.

The library function only allocates the “size” bytes of memory.


It initializes the content of memory to zeroes.

It does not initialize the content of memory to zeroes.

Number of arguments

Here there are two arguments.

Here there is only one argument.

Allocated values

The allocated region is initialized to zero.

The content of allocated memory is not changed, that is, the memory contains unpredictable or garbage values, which creates a risk.

Returns value

void pointer (void *). After a successful allocation, a pointer to the block of memory is returned.

void pointer (void *). After a successful allocation, a pointer to the block of memory is returned.

Image Courtesy: cprogrammingexpert.com

Most Searched in Business and Finance Most Searched in Games and Recreation
Most Searched in Education and References Most Searched Non-Alcoholic Drinks
P Type Semiconductor vs N Type Semiconductor
Nokia Lumia 520 vs Sony Xperia T
Microsoft Excel vs Access

Add new comment

Plain text

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.