What are the differences and similarities between malloc and calloc in C?

There are two major differences between malloc and calloc in C programming language: first, in the number of arguments. The malloc() takes a single argument, while calloc() takess two. Second, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

Both malloc and calloc are used in C language for dynamic memory allocation they obtain blocks of memory dynamically. Dynamic memory allocation is a unique feature of C language that enables us to create data types and structures of any size and length suitable to our programs. Following are the major differences and similarities between malloc and calloc in detail with their syntax and example usage.

Difference Between Malloc and Calloc

Differences between malloc and calloc
malloc calloc

The name malloc stands for memory allocation.

The name calloc stands for contiguous allocation.

void *malloc(size_t n) returns a pointer to n bytes of uninitialized storage, or NULL if the request cannot be satisfied. If the space assigned by malloc() is overrun, the results are undefined.

void *calloc(size_t n, size_t size) returns a pointer to enough free space for an array of n objects of the specified size, or NULL if the request cannot be satisfied. The storage is initialized to zero.

malloc() takes one argument that is, number of bytes.

calloc() take two arguments those are: number of blocks and size of each block.

syntax of malloc():

void *malloc(size_t n);

Allocates n bytes of memory. If the allocation succeeds, a void pointer to the allocated memory is returned. Otherwise NULL is returned.

syntax of calloc():

void *calloc(size_t n, size_t size);

Allocates a contiguous block of memory large enough to hold n elements of size bytes each. The allocated region is initialized to zero.

malloc is faster than calloc.

calloc takes little longer than malloc 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.

Similarities Between Malloc and Calloc

The pointer returned by malloc or calloc has the proper alignment for the object in question, but it must be cast into the appropriate type.

Proper alignment means the value of the returned address is guaranteed to be an even multiple of alignment. The value of alignment must be a power of two and must be greater than or equal to the size of a word.

The malloc(), calloc() functions will fail if:

  • The physical limits of the system are exceeded by n bytes of memory which cannot be allocated.
  • There is not enough memory available to allocate n bytes of memory; but the application could try again later.

Syntax of Malloc

void *malloc(size_t n);

Allocates n bytes of memory. If the allocation succeeds, a void pointer to the allocated memory is returned. Otherwise NULL is returned.

/* Allocate memory for an int. */
int *ptr = (int*) malloc(sizeof (int)); 
if (ptr == NULL) 
{
  printf("Could not allocate memory\n");
  exit(-1);
}
else
  printf("Memory allocated successfully.\n");

Syntax of Calloc

void *calloc(size_t n, size_t size);

Allocates a contiguous block of memory large enough to hold n elements of size bytes each. The allocated region is initialized to zero.

/* Allocating memory for an array of 10 elements of type int. */
int *ptr = (int*) calloc(10 ,sizeof (int));
if (ptr == NULL) 
{
  printf("Could not allocate memory\n");
  exit(-1);
} 
else
  printf("Memory allocated successfully.\n");

Hope you have enjoyed reading differences and similarities between malloc and calloc. Both functions in are used for dynamic memory allocation. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!




Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

WannaCry ransom notice analysis suggests Chinese linkPosted on Monday May 29, 2017

Researchers say the WannaCry ransom note was poorly translated - possibly using Google Translate.

BA boss 'won't resign' over flight chaosPosted on Monday May 29, 2017

Chief executive Alex Cruz says flight disruption at Heathrow and Gatwick had nothing to do with cost cutting.

Hay Festival 2017: Stephen Fry's warning for the webPosted on Sunday May 28, 2017

Delivering a lecture at the Hay Festival, the actor said society could face "dire consequences".

Courtesy BBC News