C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Dynamic Memory Allocation - realloc function

C Programming

← Prev

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.

← Prev