C realloc() Function Tutorial

  • Post author:
  • Post category:C

In this section, we will learn what the realloc() function is and how to use it in C.

Not: we’re assuming you know how to allocate dynamic memories via the call to either `malloc()` or `calloc()` functions.

C realloc() Function

There are times that we want to resize a memory space that is dynamically allocated via the call to either `calloc()` or `malloc()` function.

For example, a dynamically allocated memory has enough space to hold 100 integer elements, but then we realized at runtime the program needs more space in order to store 10 more integer elements!

This is where we can use the `realloc()` function.

Also, the prototype of the `realloc()` function exists in the `stdlib.h` header file and so we need to include the header file in order to use the function.

C realloc() Function Syntax:

Here’s the prototype of the function:

void *realloc(void *ptr, size_t size)

C realloc() Function Parameters:

This function takes 2 arguments:

  1. The first argument is the memory address of the dynamic memory space allocated via the call to `calloc` or `malloc` function that we want to resize.
  2. The second argument of the function is the number of bytes we want to add to this dynamic memory.

Notes:

  • If we set 0 for the second argument, it will cause the deallocation of the memory space that is already set aside for the address we put as the first argument.
  • Also, the second argument is the sum of the memory space that is already allocated + the size of new memory space that is needed. For example, if we need 10 bytes and the allocated memory space is already 20 bytes, we need to set the value to 30 bytes so to hold the previously allocated memory and add 10 new bytes as well.

C realloc() Function Return Value:

The returned value of this function in a successful operation is a pointer to the starting point of the reallocated memory space, otherwise a NULL pointer if there was a problem.

Note: the returned data-type is a general pointer and so we need to cast to the pointer of the correct type.

Example: using realloc() function in C

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

void printNumbers(int size);
void assignNumbers(int size);
void addNumbers(int size);
int *pointer;
int main() {

    int size;
    printf("Please enter the number of elements: \n");
    scanf("%d",&size);
    pointer = (int *) calloc(size , sizeof(int));
    assignNumbers(size);

    printf("The values stored in the array are: \n");
    printNumbers(size);

    printf("\nDo you want to add 5 more elements to the array? 1 (yes) 2(No)\n");
    int check;
    scanf("%d",&check);
    if (check == 1){
       pointer = (int *) realloc(pointer, (5+size) * sizeof(int)) ;

    }

    addNumbers(size);
    printNumbers(size+5);
    free(pointer);
return 0;

}

void addNumbers(int size){
    for (int i  = size; i<size+5 ; i++){
        *(pointer +i) = i;
    }
}
void printNumbers(int size){
    for (int i = 0; i<size ; i++){
        printf("%d",*(pointer+i));
    }
}
void assignNumbers(int size){
    for (int i  = 0; i<size ; i++){
        *(pointer +i) = i;
    }
}

Output:

Please enter the number of elements:

10

The values stored in the array are:

0123456789

Do you want to add 5 more elements to the array? 1 (yes) 2(No)

1

01234567891011121314

In this example we allocated enough memory space that can hold 10 integer elements, then later in the program while it is running, via the call to the `realloc()` function, we increased the number of elements the allocated memory space could hold by 5 and inserted 5 more elements afterwards.

Note: be careful when changing the address of the pointer to the allocated dynamic memory, otherwise you might overwrite the content that is already existing in the memory.

Leave a Reply