C library - realloc() function



The C stdlib library realloc() function is used to reallocate the size of dynamically allocated memory. It allows us to allocates or reserves a new memory block after deallocating the previously allocated memory.

The function realloc() is used for optimizing the memory uses. If we find that allocated memory is too large or too small. We can use the realloc() to adjust the size of the allocated memory.

This function will only work when the memory has been dynamically allocated before using realloc() function.

Syntax

Following is the C library syntax of the realloc() function −

void *realloc(void *ptr, size_t size)

Parameters

This function accepts following parameter −

  • ptr − It represents a pointer to a memory block to be reallocated. That is previously allocated by the 'malloc()', 'calloc()', and 'realloc()'.

  • size − It represents the new size for the memory block, in bytes. If the size is zero, pointer is not null. A new minimum sized object is allocated, original object will freed.

Return Value

This function returns a pointer to the newly allocated memory. Otherwise 'NULL' if the function fails to allocate the requested block of memory.

Example 1

In this example, we use the realloc() function without dynamically allocated the memory.

#include <stdio.h>
#include <stdlib.h>
int main() 
{ 
   // initialize an array of size 10
   int arr[10];
   // store the array to pointer
   int *ptr = arr;
   // new pointer for reallocating 
   int *new_ptr; 
	
   arr[0] = 5; 
   arr[1] = 10;	 
	
   // use the new pointer  
   new_ptr = (int *)realloc(ptr, sizeof(int)*5); 
   *(new_ptr + 2) = 30; 
   
   int i;	
   for(i = 0; i < 5; i++){
      printf("%d ", *(new_ptr + i)); 	
   }
   
   // use getchar to get the charcter
   // if memory is allocated
   getchar(); 
   return 0; 
} 

Output

Following is the output, when we use realloc() without dynamically allocated memory.

warning: 'realloc' called on unallocated object 'arr' [-Wfree-nonheap-object]
new_ptr = (int *)realloc(ptr, sizeof(int)*5);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~
note: declared here
int arr[10];

Segmentation fault

Example 2

The below example uses realloc() to increase the size of the memory block.

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

int main() {
   // Initially allocate memory for 2 integers
   int *ptr = malloc(2 * sizeof(int));
   if (ptr == NULL) {
      fprintf(stderr, "Memory allocation failed\n");
      return 1;
   }

   ptr[0] = 5;
   ptr[1] = 10;

   // increase the size of the integer
   int *ptr_new = realloc(ptr, 3 * sizeof(int));
   if (ptr_new == NULL) {
      fprintf(stderr, "Memory reallocation failed\n");
      free(ptr);
      return 1;
   }

   ptr_new[2] = 15;
   
   int i;
   // Display the array
   for (i = 0; i < 3; i++) {
      printf("%d ", ptr_new[i]);
   }

   free(ptr_new);
   return 0;
}

Output

Following is the output −

5 10 15

Example 3

Let's create another example, we dynamically stores the array element to the pointer after that we increase the size of array by 7.

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

int main() {
   int *ptr, i;
   
   printf("Initial size of the array is 4\n");
   
   // Allocate memory for 4 integers
   ptr = (int*)calloc(4, sizeof(int));
   
   if(ptr == NULL) {
      printf("Memory allocation failed");
      exit(1);
   }
   
   // initial array
   int initialArray[4] = {1, 2, 3, 4};
   
   // Copy array into ptr
   for(i = 0; i < 4; i++) {
      ptr[i] = initialArray[i];
   }
   
   printf("Increasing the size of the array by 3 elements ...\n");
   
   // Increase the size of the array to 7 elements
   // use realloc
   ptr = (int*)realloc(ptr, 7 * sizeof(int));
   
   if(ptr == NULL) {
      printf("Memory allocation failed");
      exit(1); 
   }
   
   // additional elements
   int additionalElements[3] = {5, 6, 7};
   
   // Copy additional elements into ptr
   for(i = 4; i < 7; i++) {
      ptr[i] = additionalElements[i - 4];
   }
   
   printf("Final array: \n");
   
   // display the final array
   for(i = 0; i < 7; i++) {
      printf("%d ", *(ptr+i));
   }
   free(ptr);
   return 0;
}

Output

Following is the output −

Initial size of the array is 4
Increasing the size of the array by 3 elements ...
Final array:
1 2 3 4 5 6 7
Advertisements