home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / memory / realloc1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-31  |  1.3 KB  |  45 lines

  1. /* REALLOC.C illustrates heap functions:
  2.  *      calloc      realloc         _expand
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <malloc.h>
  7.  
  8. main()
  9. {
  10.     int  *bufint;
  11.     char *bufchar;
  12.  
  13.     printf( "Allocate two 512 element buffers\n" );
  14.     if( (bufint = (int *)calloc( 512, sizeof( int ) )) == NULL )
  15.         exit( 1 );
  16.     printf( "Allocated %d bytes at %Fp\n",
  17.             _msize( bufint ), (void far *)bufint );
  18.  
  19.     if( (bufchar = (char *)calloc( 512, sizeof( char ) )) == NULL )
  20.         exit( 1 );
  21.     printf( "Allocated %d bytes at %Fp\n",
  22.             _msize( bufchar ), (void far *)bufchar );
  23.  
  24.     /* Expand the second buffer, reallocate the first. Note that trying
  25.      * to expand the first buffer would fail because the second buffer
  26.      * would be in the way.
  27.      */
  28.     if( (bufchar = (char *)_expand( bufchar, 1024 )) == NULL )
  29.         printf( "Can't expand" );
  30.     else
  31.         printf( "Expanded block to %d bytes at %Fp\n",
  32.                 _msize( bufchar ), (void far *)bufchar );
  33.  
  34.     if( (bufint = (int *)realloc( bufint, 1024 * sizeof( int ) )) == NULL )
  35.         printf( "Can't reallocate" );
  36.     else
  37.         printf( "Reallocated block to %d bytes at %Fp\n",
  38.                 _msize( bufint ), (void far *)bufint );
  39.  
  40.     /* Free memory */
  41.     free( bufint );
  42.     free( bufchar );
  43.     exit( 0 );
  44. }
  45.