realloc

realloc():

  • This is used to change the size of the memory which is already allocated by either malloc() or calloc().
  • It deallocates the memory pointer by ‘ptr’ and allocates a new memory space with the specified number of bytes given by size parameter.
  • In general, it is often used to enlarge the existing memory region.
  • The pointer returned is of type void hence we can typecast such pointer to any type as per to need.
  • It returns NULL in case of failure and the old memory is left intact in case of failure or the base address is returned in case of success.
  • The content of old memory is copied into new memory. If old memory happens to be less than new extra memory can have non-deterministic(garbage) values.
  • realloc() should always be used with dynamically allocated memory, if not the behavior is not defined.

Internal working of realloc():

  • If there is enough free space behind the current block to fulfill the request, extend the current block and return a pointer to the beginning of the block which is same as that of old one. In this case old block is not freed.
  • Else if there is a large enough free block elsewhere, then allocate that block, copy the data from the old block over, old block is freed automatically and return a pointer to the beginning of the new block.
  • The rest of the block of new memory will be uninitialized.
  • Else report failure by returning NULL and if it fails the old memory will be as it is neither freed nor moved.
  • If success it returns address of the new block (if not tailed after the old one) or same address as that of old one is returned.
  • It fails if there is not enough memory to allocate, the memory should be continuous.

Syntax:

(void*) realloc ( void *ptr, size_t new_size)

Where ptr points to the old address which needs to be resized and the new size is the new size of memory blocks.

realloc in c

Examples:

Sample Program1:

This realloc() works like malloc() and allocates memory of 10 bytes

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

int main ()
{
  int *ptr = NULL;
  ptr = realloc (ptr, 10);
  if (ptr == NULL)
    printf ("\n Realloc failed\n");
  else
    printf ("\n Realloc successful\n");
  return 0;
}

Output:

Realloc successful

Sample Program3:

This does realloc() to resize the memory from 10 bytes to 15 bytes

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

int main ()
{
  int *ptr = (int*) malloc(10);
  if (ptr == NULL)
      printf ("\n Memory allocation failed\n");

  /*Never overwrite the old memory */
  int *new_ptr = (int*) realloc (ptr, 15);
  if (new_ptr == NULL)
      printf ("\n Realloc failed\n");
 
  ptr = new_ptr;
  free (new_ptr);
  return 0;
}

Sampel Program:3

This program first uses calloc() to allocate stack of size 5, store the values and later use the realloc() to increment the size of stack to 6.

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

int main ()
{
  int capicity = 5;
  int *stack, *org_stack;
  int i, j = 0;
  
  stack = (int*) calloc(capicity, sizeof(int));
  if (!stack)
     printf("\n Allocation failure\n");
     
  org_stack = stack;
  int capicity_org = capicity;
  /* Filling the stack */
  for (i = 0; i < capicity; i++)
  {
    *stack = i;
     stack++;
  }
  
  /* Displaying the stack */
  printf("\n Content after calloc is: ");
  for (j = 0; j < capicity_org; j++)
  {
    printf(" %d ", *(org_stack + j));
    
  }
  
  /* New Capicty */
  int new_capcity = 6;
  int *new_stack;
  new_stack = (int *) realloc(org_stack, (new_capcity * sizeof(int)));
  if (!new_stack)
     stack = new_stack;

  /* Putting the new value */
  *(org_stack + 5) =5;

  /*Display the complete stack */
  printf ("\n Content after realloc is: ");
  for (j = 0; j <new_capcity; j++)
     printf(" %d ", *(org_stack + j));

  free (new_stack);
  printf ("\n");
  return 0;
}

Output:

Content after calloc is: 0 1 2 3 4
Content after realloc is: 0 1 2 3 4 5

Note:

Never overwrite the old memory, because if realloc() fails and returns NULL old address will be overwritten. Hence use the new pointer and if successful copy that to the old one and free the new one.

Relevant Posts:




Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: