C Syntax of realloc()

Syntax of realloc() in C

The realloc() function in C is used to dynamically resize a previously allocated block of memory. It takes two arguments: a pointer to the previously allocated memory block and the new size in bytes that the block should be resized to.

The syntax of realloc() is as follows:

ptr = realloc(ptr, newSize);
  • ptr: A pointer to the previously allocated memory block.
  • newSize: The new size in bytes that the block should be resized to.

The realloc() function returns a pointer to the newly allocated memory block. If the reallocation fails, it returns NULL. It is important to note that if the reallocation fails, the original memory block is left unchanged.

Explanation of each step:

  1. Allocate memory using malloc() or calloc(): Before using realloc(), you need to allocate memory using either malloc() or calloc(). This initial memory allocation can be done using the following syntax:

c ptr = malloc(initialSize);

or

c ptr = calloc(numElements, elementSize);

where ptr is a pointer to the allocated memory block.

  1. Use realloc() to resize the memory block: To resize the memory block, you can use the realloc() function. The syntax for resizing the memory block is as follows:

c ptr = realloc(ptr, newSize);

where ptr is the pointer to the previously allocated memory block, and newSize is the new size in bytes that the block should be resized to.

It is important to assign the return value of realloc() back to the same pointer variable ptr. This is because if the reallocation fails, the original memory block is left unchanged, and the original pointer is still valid.

  1. Check if the reallocation was successful: After calling realloc(), you should check if the reallocation was successful by checking if the returned pointer is NULL. If the reallocation fails, it means that there is not enough memory available to resize the block.

c if (ptr == NULL) { // Reallocation failed // Handle the error }

If the reallocation is successful, the new memory block will be returned, and the original memory block will be freed.

  1. Free the memory block if necessary: If the reallocation was successful and you no longer need the original memory block, you should free it using the free() function.

c free(ptr);

This step is important to avoid memory leaks.

It is worth noting that realloc() can also be used to allocate memory if the pointer passed to it is NULL. In this case, realloc(NULL, newSize) is equivalent to malloc(newSize).

Example:

Here is an example that demonstrates the usage of realloc():

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

int main() {
    int* ptr;
    int initialSize = 5;
    int newSize = 10;

    // Allocate memory using malloc
    ptr = malloc(initialSize * sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Resize the memory block using realloc
    ptr = realloc(ptr, newSize * sizeof(int));
    if (ptr == NULL) {
        printf("Memory reallocation failed\n");
        return 1;
    }

    // Free the memory block
    free(ptr);

    return 0;
}

In this example, we first allocate memory for an array of integers using malloc(). Then, we resize the memory block using realloc() to accommodate a larger array. Finally, we free the memory block using free().

Please note that this is a simplified example for demonstration purposes, and error handling and other considerations may be necessary in real-world scenarios.

[1]