Dynamic Memory Allocation in C using Realloc

Dynamic memory allocation using realloc :

On this page we will discuss about dynamic memory allocation using realloc. Realloc function is also know as reallocation. As the name suggests realloc function is use to re-allocate the data from old memory  block to new memory block. Basically realloc function is used to add more memory to the previously allocated memory block.
memory allocation using realloc

Dynamic memory allocation using realloc function

Realloc( ) function is used to change the size of the memory block without losing the old data.It is a built in function declared in stdlib.h .As the name itself suggests realloc( ) means reallocation.

Syntax:

void*realloc(void*ptr, newsize);

As we can see from the synatx realloc( ) will return a void pointer.Apart from this,It requires two arguments.The first is a pointer to the previously allocated memory and the another one is new size.On failure, realloc( ) returns NULL pointer.

Example:

int *ptr=(int*)malloc(sizeof(int));
ptr=(int*)realloc(ptr,2*sizeof(int));

In the above example, let’s say we have malloc( ) function which allocates memory for one integer. If we use the realloc( ) function in this case the pointr to the previously allocated memory is required and second argument is new size.

Some more Information:

  • This will allocate memory space of 2*sizeof(int) (i.e twice of what previously allocated)
  • Also this function moves the content of old block to a new memory block and the data of old block is not lost.
  • We may lose the data when the new size is smaller than the old size.
  • The bytes which are allocated newly by realloc function will not get initialized.

Programming Example of realloc :

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

int main ()
{
  int *ptr;
  int a, j;
  a = 6;			//number of elements


  ptr = (int *) calloc (a, sizeof (int));	//run time allocation

  if (ptr == NULL)
    {
      printf ("Memory not allocated.\n");
      exit (0);
    }
  else
    {
      printf ("Memory successfully allocated using calloc.\n");
      for (j = 0; j < a; ++j)
	{
	  ptr[j] = j + 1;
	}
      printf ("The elements of the array are: ");
      for (j = 0; j < a; ++j)
	{
	  printf ("%d, ", ptr[j]);
	}
      a = 12;			//new size


      ptr = realloc (ptr, a * sizeof (int));	//reallocation using realloc()

      printf ("\nMemory successfully re-allocated using realloc.\n");

      for (j = 5; j < a; ++j)
	{
	  ptr[j] = j + 1;
	}
      printf ("The elements of the array are: ");
      for (j = 0; j < a; ++j)
	{
	  printf ("%d, ", ptr[j]);
	}

      free (ptr);		//deallocate the pointer
    }

  return 0;
}

Output:

Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 
Memory successfully re-allocated using realloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription