### realloc -- Change Reserved Storage Block Size

Format

>     #include <stdlib.h>  /* also in <malloc.h> */
>     void *realloc(void *ptr, size_t size);

Language Level: ANSI, POSIX, XPG4, Extension
realloc changes the size of a previously reserved storage block. The ptr argument points to the beginning of the block. The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes.

realloc allocates the new block from the same heap the original block was in.

If ptr is NULL, realloc reserves a block of storage of size bytes from the current thread's default heap (equivalent to calling malloc(size)).

If size is 0 and the ptr is not NULL, realloc frees the storage allocated to ptr and returns NULL.

Return Value
realloc returns a pointer to the reallocated storage block. The storage location of the block may be moved by the realloc function. Thus, the ptr argument to realloc is not necessarily the same as the return value.

If size is 0, realloc returns NULL. If there is not enough storage to expand the block to the given size, the original block is unchanged and realloc returns NULL.

The storage to which the return value points is aligned for storage of any type of object.

Example
This example allocates storage for the prompted size of array and then uses realloc to reallocate the block to hold the new size of the array. The contents of the array are printed after each allocation.

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

    int main(void)
    {
      long * array;    /* start of the array */
      long * ptr;      /* pointer to array   */
      int    i;        /* index variable     */
      int  num1, num2; /* number of entries of the array */

      void print_array( long *ptr_array, int size);

      printf( "Enter the size of the array\n" );
      scanf( "%i", &num1 );

      /* allocate num1 entries using malloc() */
      if ( (array = (long *) malloc( num1 * sizeof( long ))) != NULL )
      {
         for ( ptr = array, i = 0; i < num1 ; ++i ) /* assign values */
            *ptr++ = i;
         print_array( array, num1 );
         printf("\n");
      }
      else { /*  malloc error  */
        perror( "Out of storage" );
        abort();
      }

      /* Change the size of the array ... */
      printf( "Enter the size of the new array\n" );
      scanf( "%i", &num2);

      if ( (array = (long *) realloc( array, num2* sizeof( long ))) != NULL )
      {
         for ( ptr = array + num1, i = num1; i <= num2; ++i )
            *ptr++ = i + 2000;  /* assign values to new elements */
         print_array( array, num2 );
      }

      else { /* realloc error */
        perror( "Out of storage" );
        abort();
      }
      return 0;

      /***********************************************************************
         The output should be similar to:

         Enter the size of the array
         2
         The array of size 2 is:
         array[ 0 ] = 0
         array[ 1 ] = 1
         Enter the size of the new array
         4
         The array of size 4 is:
         array[ 0 ] = 0
         array[ 1 ] = 1
         array[ 2 ] = 2002
         array[ 3 ] = 2003
      **********************************************************************/
    }

    void print_array( long  * ptr_array, int size )
    {
      int i;
      long * index = ptr_array;

      printf("The array of size %d is:\n", size);
      for ( i = 0; i < size; ++i )           /* print the array out    */
        printf( "  array[ %i ] = %li\n", i, ptr_array[i] );
    }

calloc -- Reserve and Initialize Storage
_debug_realloc -- Reallocate Memory Block
_debug_trealloc -- Reallocate Tiled Memory
free -- Release Storage Blocks
malloc -- Reserve Storage Block
_msize -- Return Number of Bytes Allocated
_trealloc -- Reallocate Tiled Storage Block
<malloc.h>
<stdlib.h>