C calloc / realloc

C calloc / realloc – Memory Allocation Functions

In C, the calloc() function is used to allocate memory and initialize it to zero, while the realloc() function allows you to resize a previously allocated memory block. These functions are used to manage memory dynamically during runtime, providing flexibility for handling varying data sizes.

🔹 calloc() – Memory Allocation and Initialization

The calloc() function allocates a block of memory for an array of elements and initializes each byte to zero. It takes two arguments: the number of elements and the size of each element.

📝 Example 1: Using calloc()

This example demonstrates how to use calloc() to allocate memory for an array of integers and initialize them to zero.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr;
    arr = (int *)calloc(5, sizeof(int)); // Allocating memory for 5 integers and initializing them to 0

    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Printing values (they should all be 0)
    for (int i = 0; i < 5; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    free(arr); // Freeing the allocated memory
    return 0;
}

Try It Now

🔹 realloc() - Resizing Memory Blocks

The realloc() function is used to resize a previously allocated memory block. It can either expand or shrink the size of the memory block, depending on the new size passed as an argument. If the block is resized successfully, it returns a pointer to the new memory block.

📝 Example 2: Using realloc()

This example demonstrates how to use realloc() to resize an array dynamically. The initial array is allocated memory for 3 elements, and then it is resized to hold 5 elements.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr;
    arr = (int *)malloc(3 * sizeof(int)); // Allocating memory for 3 integers

    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Assigning values to the initial memory block
    for (int i = 0; i < 3; i++) {
        arr[i] = i + 1;
    }

    // Resizing memory block to hold 5 integers
    arr = (int *)realloc(arr, 5 * sizeof(int));

    if (arr == NULL) {
        printf("Memory reallocation failed\n");
        return 1;
    }

    // Assigning values to the new memory block
    for (int i = 3; i < 5; i++) {
        arr[i] = i + 1;
    }

    // Printing all values
    for (int i = 0; i < 5; i++) {
        printf("arr[%d] = %d\n", i, arr[i]);
    }

    free(arr); // Freeing the allocated memory
    return 0;
}

Try It Now

⚙️ Important Notes

  • calloc() initializes the allocated memory to zero, while malloc() does not.
  • realloc() may move the memory block to a new location if the block cannot be resized in place. Always assign the result of realloc() to the original pointer.
  • When using realloc(), check if it returns NULL, which indicates failure to resize the memory.

🎯 Recap

calloc() and realloc() are powerful tools for dynamically managing memory in C. Use calloc() when you need to allocate and initialize memory, and realloc() when you need to resize an existing memory block. Properly managing memory with these functions can help prevent memory leaks and ensure efficient memory usage in your programs.