realloc() — Change Reserved Storage Block Size


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

Language Level





The realloc() function 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.

If the ptr is NULL, realloc() reserves a block of storage of size bytes. It does not necessarily give all bits of each element an initial value of 0.

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

  1. All heap storage is associated with the activation group of the calling function. As such, storage should be allocated, deallocated, and reallocated within the same activation group. You cannot allocate heap storage within one activation group and deallocate or reallocate that storage from a different activation group. For more information about activation groups, see the ILE Concepts manual.
  2. If the Quick Pool memory manager has been enabled in the current activation group then storage is retrieved using Quick Pool memory manager. See_C_Quickpool_Init() — Initialize Quick Pool Memory Manager for more information.

Return Value

The realloc() function 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 the realloc() function is not necessarily the same as the return value.

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

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

To use teraspace storage instead of single-level store storage without changing the C source code, specify the TERASPACE(*YES *TSIFC) parameter on the compiler command. This maps the realloc() library function to _C_TS_realloc(), its teraspace storage counterpart. The maximum amount of teraspace storage that can be allocated by each call to _C_TS_realloc() is 2GB - 240, or 214743408 bytes. For additional information about teraspace storage, see the ILE Concepts manual.


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 );
  else { /*  malloc error  */
    perror( "Out of storage" );
  /* 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" );
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] );
/**** If the initial value entered is 2 and the second value entered 
      is 4, then the expected output is:
Enter the size of the array
The array of size 2 is:
  array[ 0 ] = 0
  array[ 1 ] = 1
Enter the size of the new array
The array of size 4 is:
  array[ 0 ] = 0
  array[ 1 ] = 1
  array[ 2 ] = 2002
  array[ 3 ] = 2003                                                */

Related Information