Open In App

Difference Between malloc() and calloc() with Examples

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The functions malloc() and calloc() are library functions that allocate memory dynamically. Dynamic means the memory is allocated during runtime (execution of the program) from the heap segment.

Initialization

malloc() allocates a memory block of given size (in bytes) and returns a pointer to the beginning of the block. malloc() doesn't initialize the allocated memory. If you try to read from the allocated memory without first initializing it, then you will invoke undefined behavior, which usually means the values you read will be garbage values.

calloc() allocates the memory and also initializes every byte in the allocated memory to 0. If you try to read the value of the allocated memory without initializing it, you'll get 0 as it has already been initialized to 0 by calloc().

Parameters

malloc() takes a single argument, which is the number of bytes to allocate.

Unlike malloc(), calloc() takes two arguments:

  1. Number of blocks to be allocated. 
  2. Size of each block in bytes.

Return Value

After successful allocation in malloc() and calloc(), a pointer to the block of memory is returned otherwise NULL is returned which indicates failure.

Example

The below C code demonstrates the difference between malloc and calloc functions to allocate dynamic memory.

C
// C code that demonstrates the difference
// between calloc and malloc
#include <stdio.h>
#include <stdlib.h>

int main()
{
    // Both of these allocate the same number of bytes,
    // which is the amount of bytes that is required to
    // store 5 int values.

    // The memory allocated by calloc will be
    // zero-initialized, but the memory allocated with
    // malloc will be uninitialized so reading it would be
    // undefined behavior.
    int* allocated_with_malloc = malloc(5 * sizeof(int));
    int* allocated_with_calloc = calloc(5, sizeof(int));

    // As you can see, all of the values are initialized to
    // zero.
    printf("Values of allocated_with_calloc: ");
    for (size_t i = 0; i < 5; ++i) {
        printf("%d ", allocated_with_calloc[i]);
    }
    putchar('\n');

    // This malloc requests 1 terabyte of dynamic memory,
    // which is unavailable in this case, and so the
    // allocation fails and returns NULL.
    int* failed_malloc = malloc(1000000000000);
    if (failed_malloc == NULL) {
        printf("The allocation failed, the value of "
               "failed_malloc is: %p",
               (void*)failed_malloc);
    }

    // Remember to always free dynamically allocated memory.
    free(allocated_with_malloc);
    free(allocated_with_calloc);
}

Output
Values of allocated_with_calloc: 0 0 0 0 0 
The allocation failed, the value of failed_malloc is: (nil)

Difference between malloc() and calloc() in C

Understanding the difference between malloc() and calloc() is key to dynamic memory allocation.

Let us see the differences in a tabular form:

S.No. 

malloc()

calloc()

1.

malloc() is a function that creates one block of memory of a fixed size.calloc() is a function that assigns a specified number of blocks of memory to a single variable.

2.

malloc() only takes one argumentcalloc() takes two arguments.

3.

malloc() is faster than calloc.calloc() is slower than malloc()

4.

malloc() has high time efficiencycalloc() has low time efficiency

5.

malloc() is used to indicate memory allocationcalloc() is used to indicate contiguous memory allocation

6.

Syntax : void* malloc(size_t size);Syntax : void* calloc(size_t num, size_t size);

7.

malloc() does not initialize the memory to zerocalloc() initializes the memory to zero

8.

malloc() does not add any extra memory overheadcalloc() adds some extra memory overhead

Related Articles


Next Article

Similar Reads