Difference Between Malloc () And Calloc () In C

SHARE

Malloc ()

The malloc () function stands for memory allocation. It is a function which is used to allocate a block of memory dynamically from the heap. The heap is an area of memory where something is stored.  Calloc reserves memory space of specified size and returns the null pointer pointing to the memory location. The pointer returned is usually of type void. It means that we can assign malloc function to any pointer. Malloc () is useful when you don’t know the amount of memory required during compile time.

Malloc () is part of stdlib.h and to be able to use it you need to use #include <stdlib.h>. Malloc () allocates memory at runtime. It takes the size in bytes and allocates that much space in the memory and returns a pointer to the beginning of the allocated block.

C passes by value instead of reference. Using malloc () to assign memory and then pass the pointer to another function, is more efficient than having the function recreate the structure.

What You Need To Know About Malloc ()

  • Malloc () is an abbreviation for memory allocation. It is a predefined function defined in the stdlib.h header file.
  • Malloc () is used to allocate memory during the runtime of a program.
  • Syntax of malloc ():  void*malloc(size_t  n);
  • The memory allocated is uninitialized that means it has garbage values.
  • Malloc () accommodates a single argument at a time that is, number of byte.
  • For Malloc () 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.
  • Malloc () allocates a single block of memory with given byte-size.
  • Malloc () is used for creating structures.
  • Malloc () is relatively faster than calloc ().

Calloc ()

The calloc () function stands for contagious allocation. This function is used to allocate multiple blocks of memory. It is a dynamic memory allocation function which is used to allocate the memory to complex data structures such as arrays and structures.

The calloc () function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0 then, calloc () return either NULL, or a unique pointer value that can later be successfully passed to free ().

What You Need To Know About Calloc ()

  • Calloc () is an abbreviation for contiguous allocation. It is an advancement over malloc () function.
  • Calloc () is used to allocate multiple blocks of memory of the same size dynamically.
  • Syntax of calloc ():  void*calloc(size_t  n, size_t  size);
  • Memory initialization is performed by calloc (). The memory is initialized with zero.
  • The Calloc () function takes two arguments. The 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.
  • For calloc (), 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 NULL pointer.
  • Calloc () allocates multiple blocks of memory that are contagious in memory with given byte-size.
  • Calloc () is for creating dynamic arrays.
  • Calloc () takes time to allocate multiple blocks of memory, because of the extra step of initializing the allocated memory by zero. However, in practice the difference in speed is very tiny and not recognizable.

Also Read: Difference Between Structure And Union In C

Difference Between Malloc () And Calloc () In Tabular Form

BASIS OF COMPARISON   MALLOC CALLOC
Description Malloc () is an abbreviation for memory allocation. It is a predefined function defined in the stdlib.h header file.   Calloc () is an abbreviation for contiguous allocation. It is an advancement over malloc () function.  
Use Malloc () is used to allocate memory during the runtime of a program.   Calloc () is used to allocate multiple blocks of memory of the same size dynamically.  
Syntax Syntax of malloc ():  void*malloc(size_t  n);   Syntax of calloc ():  void*calloc(size_t  n, size_t  size);  
Memory Initialization The memory allocated is uninitialized that means it has garbage values.   Memory initialization is performed by calloc (). The memory is initialized with zero.  
Number Of Arguments Malloc () accommodates a single argument at a time that is, number of byte.   The Calloc () function takes two arguments. The 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.  
Memory Allocation For Malloc () 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.   For calloc (), 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 NULL pointer.  
Memory Block Malloc () allocates a single block of memory with given byte-size.   Calloc () allocates multiple blocks of memory that are contagious in memory with given byte-size.  
Purpose Malloc () is used for creating structures.   Calloc () is for creating dynamic arrays.  
Memory Allocation Speed Malloc () is relatively faster than calloc ().   Calloc () takes time to allocate multiple blocks of memory, because of the extra step of initializing the allocated memory by zero.

Also Read: Difference Between Stack And Heap In C++

Summary

The malloc () and calloc () functions return a pointer to the allocated memory that is suitably aligned for any kind of variable. On error, these functions return NULL. NULL may also be returned by a successful call to malloc () with a size of zero or by a successful call to calloc () with nmemb or size equal to zero.