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

Bees 'go online' in ManchesterPosted on Friday September 22, 2017

Rory visits a beekeeper in Manchester who is gluing wireless chips to his bees.

The long wait for a Persian iPhone keyboardPosted on Friday September 22, 2017

The new Apple's iOS has Persian keyboard. BBC Persian's Sam Farzaneh discusses why it is an important feature.

Uber London loses licence to operatePosted on Friday September 22, 2017

Ride-hailing app Uber is "not fit and proper" to operate in London, the transport regulator says.

Courtesy BBC News

AD BLOCKER DETECTED!

Advertisements help running this site for free.


To view the content please disable AdBlocker and refresh the page.

×