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

  1. /* DOSMEM.C illustrates functions:
  2.  *      _dos_allocmem       _dos_setblock       _dos_freemem
  3.  *
  4.  * See COPY2.C for another example of _dos_allocmem and _dos_freemem.
  5.  */
  6.  
  7. #include <dos.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. main()
  12. {
  13.     char far *buf = NULL, far *p;
  14.     unsigned segbuf, maxbuf, size = 512;
  15.  
  16.     /* Allocate 512-byte buffer. Convert the size to paragraphs).
  17.      * Assign the segment to the buffer. Fill with A's.
  18.      */
  19.     if( _dos_allocmem( size >> 4, &segbuf ) )
  20.         exit( 1 );
  21.     FP_SEG( buf ) = segbuf;
  22.     for( p = buf; p < (buf + size); p++ )
  23.         *p = 'A';
  24.  
  25.     /* Double the allocation. Fill the second half with B's. */
  26.     size *= 2;
  27.     if( _dos_setblock( size >> 4, segbuf, &maxbuf ) )
  28.         exit( 1 );
  29.     FP_SEG( buf ) = segbuf;
  30.     for( p = buf + (size / 2); p < (buf + size); p++ )
  31.         *p = 'B';
  32.     *(--p) = '\0';
  33.  
  34.     printf( "Memory available: %u paragraphs\n", maxbuf );
  35.     printf( "Buffer at %Fp contains:\n%Fs", (int far *)buf, buf );
  36.  
  37.     /* Free memory */
  38.     exit( !_dos_freemem( segbuf ) );
  39. }
  40.