realloc function in C language – Reallocate / Resize Dynamic memory

realloc-function-in-c-to-resize-dynamic-memory

Introduction:

In earlier posts, we looked at static vs dynamic memory allocation, Memory allocation using the malloc() function and calloc() function. In today’s article, We will look at the Reallocation of dynamically allocated memory using the realloc function in c programming language.

Prerequisites:

Please go through the following pointers and functions programs to better understand this tutorial.

The realloc() function in C – Re-allocate Dynamically allocated memory:

As previously mentioned, The malloc() and calloc() functions are used to allocate memory, but what if you wish to modify the size of the dynamically allocated memory? This is where we use the realloc() function in the c language.

The realloc() function is used to reallocate the memory allocated by the malloc() or calloc() functions. We can decrease or increase the size of the dynamically allocated memory using the realloc() function in c. The realloc() function resizes the memory block while retaining the previous data.

Syntax of realloc() function in C language:

Here

  • ptr – Pointer to dynamically allocated memory( malloc() or calloc() allocated memory).
  • size – New size of the memory block. It can see less than or greater than the previous size.
  • void * – The realloc() function also returns the void pointer.

The realloc() function returns the pointer to the newly allocated memory block on successful reallocation.

If the realloc() fails, It will return the NULL.

Example usage of realloc() function in C Programming:

Let’s say we want to dynamically allocate memory for 5 integer numbers.

Here we used the malloc() function to allocate the memory for 5 integer variables. The rPtr pointer variable is storing the address of the first memory byte.

Now, our requirement is changed and we need to increase the dynamically allocated memory to store the 10 integers variables.

We can use the realloc() function to resize the dynamically allocated memory.

We passed the new size 10 * sizeof(int) to the realloc() function along with the pointer variable rPtr

The realloc() function resizes the memory and returns a pointer to the newly allocated memory block, The returned pointer is a void pointer, So we need to typecast it to the appropriate pointer type.

Quick Notes on realloc() function in C Language:

  • On successful resize of memory, The realloc() function returns the pointer to the newly allocated memory block. If realloc() function fails, It will return the NULL value.
  • The address of the pointer variable rPtr(Returned by the realloc() function) might change if there is not sufficient memory at the previous address. In such cases, The realloc() function moves the data to a new block of memory and returns the pointer to the newly allocated memory block. So we might get a different pointer address.
  • The newly allocated memory bytes are uninitialized, So they might contain garbage values.

Let’s look at a program to reallocate dynamically allocated memory using realloc() function in c programming language.

Resizing the dynamically allocated memory using realloc() function in C programming language:

We started the above program by dynamically allocating memory for a user-provided number of integer variables using the malloc() function. If the malloc() failed to allocate the memory, Then we terminated the program using a return statement. Otherwise, it will continue and prompt the user to provide the values for each integer variable using a for loop and store the values in the allocated memory. We then printed the integer variable on the console using another for loop.

Next, We prompted the user to provide the new size and reallocated the dynamically allocated memory using the realloc() function. To reallocate memory we need to pass the pointer variable and the new size to the realloc() function. like below

If the new size is greater than the old size. Then we prompted the user to provide the remaining values and displayed them on the screen.

Finally, We freed the allocated memory using the free() function.

Program Output:

Let’s compile and run the program.

Test 1:

realloc-function-in-c-programming-language-program-output

As we can see from the above output, Initially we allocated memory for 5 Integersusing the malloc() function, Then we resized the array to store the 9 Integers using the realloc() function.

Test 2:

In this example, We have decreased the size of the dynamically allocated memory using realloc(). The initial memory stores 7 Integers and resized memory stores only 4 Integers. ( As we reallocated memory using realloc() function, Only the 4 * sizeof(int) memory is allocated to our program.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply