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

  1. /* FREECT.C illustrates the following heap functions:
  2.  *      _freect         _memavl
  3.  */
  4.  
  5. #include <malloc.h>
  6. #include <stdio.h>
  7.  
  8. main()
  9. {
  10.     char near *bufs[64];
  11.     unsigned request, avail, i;
  12.  
  13.     printf( "Near heap bytes free: %u\n\n", _memavl() );
  14.     printf( "How many 1K buffers do you want from the near heap? " );
  15.     scanf( "%d", &request );
  16.     if( request > 64 )
  17.     {
  18.         printf( "There are only 64K in a segment.\n" );
  19.         request = 64;
  20.     }
  21.  
  22.     avail = _freect( 1024 );
  23.     request = (avail > request) ? request : avail;
  24.     printf( "You can have %d buffers\n", request );
  25.  
  26.     printf( "They are available at:\n");
  27.     for( i = 0; i < request; i++ )
  28.     {
  29.         bufs[i] = (char near *)_nmalloc( 1024 );
  30.         printf( "%2d %Fp   ", i + 1, (char far *)bufs[i] );
  31.         if( (i % 5) == 4 )
  32.             printf( "\n" );
  33.     }
  34.     printf( "\n\nNear heap bytes free: %u\n\n", _memavl() );
  35.     printf( "Freeing buffers . . ." );
  36.     for( i = request; i; i-- )
  37.         _nfree( bufs[i] );
  38.     printf( "\n\nNear heap bytes free: %u", _memavl() );
  39. }
  40.