malloc() vs calloc() – Difference Between malloc() and calloc() in C

Here in this tutorial you will learn about difference between malloc() and calloc() in C.

We all know that memory is available in limited size only, so it becomes important for us to use it efficiently. If we are writing a program that uses too much of memory resulting in wastage of memory or conversely if we allocate too little memory so that we can’t store enough information, that means we are not sure of the amount of data to be stored. In such cases, dynamic memory allocation comes to the rescue, where our program is capable to allocate whatever amount of memory it needs during the run-time of programs.

Now the question arises is that how can we achieve dynamic memory allocation in our programs?

In the C Language, we have predefined functions like calloc() and malloc() defined under the stdlib.h header file that can be used to allocate memory during the runtime of a program.

Let’s understand this awesomely useful function with simple examples:

malloc() Function

malloc() is an abbreviation for memory-allocation. It is a predefined function defined in the stdlib.h header file. It is used to allocate memory during the runtime of a program. The memory allocated is uninitialized that means it has garbage values.

Syntax:

The malloc() function takes size in bytes to be allocated as argument. If the memory allocation is successful, it returns a void pointer to the beginning of the allocated memory. This void pointer can be type-casted to any type of pointer. If the memory allocation fails due to reasons like insufficient memory, the malloc() function returns a NULL pointer.

It is the responsibility of the programmer to deallocate the allocated memory before program ends using free() or realloc() functions.

C Program Implementation of malloc() Function

Output

Memory allocation successful !!

calloc() Function

calloc() is an abbreviation for c-contiguous a-allocation. It is an advancement over the malloc() function. It is used to allocate multiple blocks of memory of the same size dynamically. The memory is initialized with zero.

Syntax:

The calloc() function takes two arguments. First argument is the number of blocks of memory to be allocated and the second argument is used to define the size of blocks in terms of bytes. If the memory allocation is successful, it returns a void pointer to the beginning of the allocated memory. This void pointer can be type-casted to any type of pointer. If the memory allocation fails due to reasons like insufficient memory, the calloc() function returns a NULL pointer.

It is the responsibility of the programmer to deallocate the allocated memory before program ends using free() or realloc() functions.

C Program Implementation of calloc() Function

Output

The values are:
Memory block 1 : 0
Memory block 2 : 0
Memory block 3 : 0
Memory block 4 : 0

malloc() vs calloc() – Difference between malloc() and calloc()

Parameters malloc() calloc()
Abbreviated for m-memory,  alloc-allocation c-contiguous,  alloc-allocation
Syntax: (void*) malloc (n * size in bytes) (void*) calloc (n , size in bytes)
Definition It is a predefined function defined in stdlib.h header file used to allocate memory dynamically in terms of bytes. It is predefined function present in stdlib.h header file used to allocate memory dynamically in terms of number of blocks of memory with given size in bytes.
No.  of Arguments It takes single argument. It takes two arguments.
No.  of memory blocks Allocates a single block of memory with given byte-size. Allocates multiple blocks of memory that are contiguous in memory with given byte-size.
Initialization It does not initialize the allocated memory. It initializes the allocated memory blocks with 0.
Garbage value Present Absent
Speed It is fast. It takes time to allocate multiple blocks of memory.
Used for Creating structures Creating dynamic Arrays

Comment down below if you have any queries related to malloc() and calloc() in C.

1 thought on “malloc() vs calloc() – Difference Between malloc() and calloc() in C”

  1. I would say that calloc allocs one block, rather than “n contiguous blocks”, because I believe that calloc can be modeled with malloc(count * size).

    If calloc is substantially slower than malloc, it should be replaced with malloc and memset.

Leave a Comment

Your email address will not be published. Required fields are marked *