calloc() vs malloc() in C

In this lecture, we will discuss about the usage details of calloc() function for memory management in C/C++.

What is calloc() function in C programming?

Using the calloc() function, we can allocate multiple blocks of memory on the heap. This is very different from the malloc() function, which is used to allocate a single chunk of memory on the heap. With calloc(), we can allocate multiple blocks of memory.

Syntax of calloc() function

The syntax for the calloc() function is as follows:

void * calloc(size_t num, size_t size);

It accepts two arguments. The first one, num, is the number of blocks that need to be allocated. The second one, size, is the size of one block. Also, calloc() initialises the allocated memory to zero. It returns a void pointer pointing to the start of the first block.

Example of calloc() function

So, using the calloc() function, we can allocate memory for N blocks. For this, we need to pass two arguments to the calloc() function. The first one is the number of blocks, in our case that will be 5, and the second is the size of each block, in our case, that will be the size of an integer.

int n = 5;

// Allocate 5 blocks of 4 bytes each
int *ptr = (int*) calloc(n, sizeof(int));

So, it will allocate 5 blocks of 4 bytes each and return a void pointer pointing to the start of the first block. Then, we can cast it to an integer pointer and assign it to the pointer ptr.

If it fails to allocate the memory, it can return null. Therefore, we should always check if the returned pointer is null or not. If it is not null, then we can directly access the individual elements in the allocated blocks. As calloc() always initializes the allocated memory to zero while allocating, we can directly print the allocated memory. On the other hand, malloc() initializes allocated memory on the heap with garbage values.

    if (ptr != NULL) 
    {
        // Memory successfully allocated using calloc
        // print the array
        for (int i = 0; i < n; ++i) 
        {
            printf("%d ", ptr[i]);
        }
        printf("\n");
    }
    else
    {
        printf("Memory not allocated.\n");
        exit(0);
    }
    

    Output:

    0 0 0 0 0 
    

    Once the memory is allocated using calloc(), and after that, when we don’t need it, we should deallocate the memory using the free() function. For this, we will pass the pointer that points to the start of the memory allocated by calloc() to the free() function, and it will deallocate the memory.

    free(ptr);
    

    The complete example is as follows,

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n = 5;
    
        // Allocate 5 blocks of 4 bytes each
        int *ptr = (int*) calloc(n, sizeof(int));
    
        if (ptr != NULL) 
        {
            // Memory successfully allocated using calloc
            // print the array
            for (int i = 0; i < n; ++i) 
            {
                printf("%d ", ptr[i]);
            }
            printf("\n");
    
            free(ptr);
        }
        else
        {
            printf("Memory not allocated.\n");
            exit(0);
        }
    
        return 0;
    }
    

    Output:

    0 0 0 0 0 
    

    Difference between malloc() & calloc() in C/C++

    Now, we will look at some differences between malloc() and calloc().

    Memory Allocation – malloc() vs calloc()

    The malloc() function allocates a single, contiguous chunk of memory. It accepts the size in bytes and allocates that much memory, returning a void pointer pointing to the allocated memory.

    // Allocate memory chunck of 20 bytes on heap
    int *ptr = (int*) malloc(5 * sizeof(int));
    

    The calloc() function, on the other hand, allocates multiple blocks of memory. It accepts the number of blocks you want to allocate and the size of each block in bytes. It also returns a pointer pointing to the start of the first block.

    // Allocate 5 blocks of 4 bytes each on heap
    int *ptr = (int*) calloc(5, sizeof(int));
    

    Memory Initialisation – malloc() vs calloc()

    When malloc() allocates memory on the heap, all the values it contains are garbage values. Whereas, calloc(), after allocating the memory, initializes the memory blocks to zero.

    Number of Arguments – malloc() vs calloc()

    The malloc() function accepts one argument: the number of bytes to be allocated.

    void* malloc(size_t size);
    

    Whereas, the calloc() accepts two arguments: the first is the number of blocks to be allocated and the second is the size of each block.

    void * calloc(size_t num, size_t size);
    

    Memory deallocation – malloc() vs calloc()

    Once memory is allocated by both malloc() and calloc(), we need to deallocate it using the free() function in the end. Like this,

    // Allocate memory chunck of 20 bytes on heap
    int *ptr1 = (int*) malloc(5 * sizeof(int));
    
    // Allocate 5 blocks of 4 bytes each on heap
    int *ptr2 = (int*) calloc(5, sizeof(int));
    
    // Deallocate memory on heap
    free(ptr1);
    
    // Deallocate memory on heap
    free(ptr2);
    

    Summary

    We can use the calloc() function to allocate multiple blocks of memory on the heap.

    Scroll to Top