C - Dynamic Memory Allocation - realloc function

C Programming

Share

In this tutorial we will learn about realloc function to dynamically reallocate memory in C programming language.

The realloc function

We use the realloc function to change the memory size of the previously allocated memory space.

Realloc syntax

Following is the syntax of the realloc function.

ptr = realloc(ptr, new_size);

Where, ptr is a pointer pointing at the allocated memory location. new_size is the size of the new allocation.

Points to note

Following are the points to note when using realloc function.

  • The functions allocates a new memory space of size new_size and returns a pointer that points at the first address of the newly reallocated memory space.
  • The new_size may be larger than or less than the previously allocated memory space.
  • The newly reallocated memory block may not start from the same location as the old allocated space.
  • If the relloacted memory size is bigger than the original size and if there is no space to accommodate the new size in the same region then, this function will allocate the new memory block elsewhere, and will move the content of the old block to the new location.
  • If this function fails to get the new size memory location then it will return NULL.

Write a program in C to reallocate previously allocated memory space

In the following code we are first allocating 6 bytes of memory space of type char using the malloc function.

Then we are taking 5 letters word from the user as input and saving it in the allocated memory space.

After that we are reallocating 9 bytes of memory space of type char using the realloc function.

Then, we are taking 8 letters word from the user as input and saving it in the newly reallocated memory space.

The last byte is used to store the NULL \0 character to mark the end of the string. So, 6 bytes space can hold 5 bytes text and similarly, 9 bytes will hold 8 bytes text.

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

int main(void) {
    
  // character pointer
  char *cptr = NULL;
  
  // allocate memory
  cptr = (char *) malloc (6 * sizeof(char));
  
  // if failed
  if (cptr == NULL) {
    printf("Failed to allocate memory space. Terminating program...");
    return -1;
  }
  
  // get some text
  printf("Enter 5 letters word: ");
  scanf("%s", cptr);
  
  // display
  printf("Word you entered: %s\n", cptr);
  
  // reallocate memory
  cptr = realloc(cptr, (9 * sizeof(char)));
  
  // if failed
  if (cptr == NULL) {
    printf("Failed to reallocate new memory space. Terminating program...");
    return -1;
  }
  
  // content
  printf("\nContent of the allocated memory space after reallocation:\n");
  printf("%s\n\n", cptr);
  
  // get some new text
  printf("Enter 8 letters word: ");
  scanf("%s", cptr);
  
  // display
  printf("Word you entered: %s\n", cptr);
  
  // free memory space
  free(cptr);

  return 0;
}

Output:

Enter 5 letters word: hello
Word you entered: hello

Content of the allocated memory space after reallocation:
hello

Enter 8 letters word: computer
Word you entered: computer

We can represent the allocated memory via malloc function as follows.



And we can show the reallocated memory via realloc function as follows.



Share