Difference Between Malloc and Calloc in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Malloc and Calloc functions are used for the allocation of memory during the runtime of a C program. But as they both are different in names obviously there are a few differences too.

The difference between Malloc() and calloc() in C programming in many ways: - the main is that they have a different number of arguments. The other difference between malloc() and calloc() in C is there is no initialization in Malloc() while initialization is done to '0' in calloc().

So, In this article, we will be looking more at difference between malloc() and calloc() in C programming and about malloc() and calloc() functions.

What is Dynamic Memory Allocation?

The allocation of memory during the run time is known as Dynamic Memory Allocation. There are four standard libraries where dynamic memory is allocated - calloc(), malloc(), free(), realloc().

You can read more about Dynamic memory allocation in C.

What is Malloc()?

Memory allocation is the full form of malloc, the name suggests that a block of dynamic memory that has a specific size is allocated in malloc. Malloc function returns a null pointer that is pointed toward the memory location. The pointer return type of malloc is generally void which means the malloc function can be assigned to any pointer.

Syntax of Malloc()

The syntax of malloc() function in C: -

The size here means that a specified byte size of memory is allocated. If the size is allocated then a void pointer is returned otherwise it will return NULL.

What is Calloc()?

The calloc function works similarly to the malloc function the main difference is a specified memory block is allocated during runtime in calloc().

Syntax of Calloc()

The syntax of calloc() function: -

Calloc function allocates a contagious block of memory large enough to hold each size byte of num elements. The space allocated to the memory block is initialized to '0'.

Difference Between Malloc and Calloc in C

Basis of differencesMallocCalloc
DefineMemory Allocation is the full form of malloc which means a single dynamic memory block is allocated during runtime.Contiguous Allocation is the full form of calloc which means multiple memory blocks are allocated to a single variable.
Syntaxvoid *malloc(size_t size);void *calloc(size_t n, size_t size)
ParametersMalloc takes only one parameter.Calloc takes two parameters.
InitializationWhen we try to access the block of memory allocated, the garbage value is returned because the malloc() function doesn't initialize allocated memory.When we try to access the memory blocks we get the result as '0' because calloc initializes the allocated memory block to '0'.
SpeedMalloc does its job quickly.Calloc is slow in comparison to Malloc.
Argument typesMalloc takes the number of bytes as an argument.Calloc takes both the number of blocks and the size of the block as an argument.
SecurityIt is not secure as compare to calloc.It is secure to use compared to malloc.
Use caseMalloc is suitable when memory content initialization is not necessary, and the program is dealing with a single block of memory.Calloc is useful when the program requires multiple blocks of memory, especially when handling arrays or structures that need to be initialized to zero.

Why Use Malloc()?

Malloc can be used when: -

  • You want to allocate a single memory block during runtime.
  • If you want to allocate a large size to the memory block allocated.
  • The pointer is returned to the first byte of the allocated memory block.
  • The memory block of the size bytes is allocated from the heap.
  • The memory can be allocated as it is needed by the developers in an exact amount.
  • You want objects allocated that exist beyond the execution of the current memory block.

Why Use Calloc()?

We use calloc when: -

  • You want to initialize the dynamic memory allocated during runtime to '0'.
  • You want to prevent the overflow that happens in malloc().
  • You can access the memory heap when calloc returns a pointer.

Example of Malloc in C

In this example, we will try to allocate a memory block to char. If it succeeds, it will print the message that The memory is allocated otherwise it will return NULL.

Output

We create a pointer that is used to allocate a memory block where we can also specify the number of memory blocks. If the value of the pointer is NULL then that means the memory could not be allocated else the memory was allocated to the pointer. During the time of runtime, a memory block was allocated to char successfully.

Example of Calloc() in C

In this example, we will allocate 10 elements that hold 10 size bytes each.

Output We create a pointer that is used to allocate the memory block using calloc function where we set the byte size for each float element to 10 and create 10 memory blocks. If the value of pointer is NULL then it means the memory could not be allocated else the memory was allocated during runtime. Float is allocated 10 memory blocks of size float successfully.

Conclusion

  • The difference between malloc and calloc in C are few like they differ in Speed, and argument types.
  • Malloc function is allocated a single block of dynamic memory during runtime.
  • In the calloc function the dynamic memory allocated can have the size allocated by us as well as we can allocate multiple memory blocks.
  • Malloc function takes only one argument which is the number of bytes while calloc takes two arguments.
  • There is no initialization to the allocated memory in malloc while the allocated memory block is initialized to '0' in Calloc().