Use of realloc()

Last Updated : 20 Jul, 2026

realloc() is a standard library function used to resize a previously dynamically allocated memory block.

  • It can increase or decrease the size of memory while preserving the existing data up to the smaller of the old and new sizes.
  • It should only be used with memory allocated using malloc(), calloc(), or realloc().
C
#include <stdio.h>
#include <stdlib.h>

int main(){

    int *arr = (int *)malloc(3 * sizeof(int));

    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;

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

    arr[3] = 40;
    arr[4] = 50;

    for (int i = 0; i < 5; i++)
        printf("%d ", arr[i]);

    free(arr);

    return 0;
}

Output
10 20 30 40 50 

Explanation: In the above example, memory initially allocated for 3 integers is resized to store 5 integers using realloc().

Syntax

void *realloc(void *ptr, size_t size);

Parameters:

  • ptr: Pointer to the previously allocated memory block.
  • new_size: New size (in bytes) for the memory block.

Return Value:

  • Returns a pointer to the resized memory block on success.
  • Returns NULL if memory allocation fails (the original memory remains unchanged).

Note: realloc() should only be used on memory that was previously allocated using malloc(), calloc(), or realloc(). Using realloc() on memory that was not dynamically allocated results in undefined behavior.

How realloc() Works

Depending on memory availability, realloc() behaves in one of the following ways:

  • If there is enough free memory after the current block, it expands the existing block in place.
  • Otherwise, it allocates a new memory block, copies the existing data, frees the old block, and returns the address of the new block.
  • If the new size is smaller than the current size, the memory block is shrunk while preserving the remaining data.
  • If ptr is NULL, realloc() behaves exactly like malloc().
  • If new_size is 0, the allocated memory is freed (implementation-defined; commonly returns NULL).

Program: Incorrect Use of realloc()

C
#include <stdio.h>
#include <stdlib.h>
int main()
{
   int arr[2], i;
   int *ptr = arr;
   int *ptr_new;
   
   arr[0] = 10; 
   arr[1] = 20;      
   
   // incorrect use of new_ptr: undefined behaviour
   ptr_new = (int *)realloc(ptr, sizeof(int)*3);
   *(ptr_new + 2) = 30;
   
   for(i = 0; i < 3; i++)
     printf("%d ", *(ptr_new + i));

   getchar();
   return 0;
}
Abort signal from abort(3) (SIGABRT)
realloc(): invalid pointer
/bin/bash: line 1: 36 Aborted (core dumped) ./Solution < input.txt

Program 2: Correct Use of realloc()

C
#include <stdio.h>
#include <stdlib.h>
int main()
{
   int *ptr = (int *)malloc(sizeof(int)*2);
   int i;
   int *ptr_new;
   
   *ptr = 10; 
   *(ptr + 1) = 20;
   
   ptr_new = (int *)realloc(ptr, sizeof(int)*3);
   *(ptr_new + 2) = 30;
   for(i = 0; i < 3; i++)
       printf("%d ", *(ptr_new + i));

   getchar();
   return 0;
}

Output
10 20 30 

Advantages of realloc()

realloc() provides a convenient way to resize dynamically allocated memory while preserving existing data.

  • It resizes an existing memory block without manually allocating and copying memory.
  • It preserves the existing data up to the smaller of the old and new memory sizes.
  • It can improve performance by expanding the existing memory block in place when possible.

Common Mistakes While Using realloc()

The following are some common mistakes that programmers should avoid while using realloc():

  • Using realloc() on memory that was not dynamically allocated (such as stack or static memory).
  • Not checking whether realloc() returns NULL before using the returned pointer.
  • Assigning the result of realloc() directly to the original pointer, which may cause a memory leak if reallocation fails.
Comment