home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l350 / 3.ddi / EXAMPLES / MSC / PHAR_MEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-29  |  923 b   |  48 lines

  1. // PHAR_MEM.C
  2. // usage:  phar_mem [nmegabytes]
  3. // requires at least one megabyte of memory
  4.  
  5. #include <malloc.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. void fail(char *s)
  10. {
  11.     puts(s);
  12.     exit(1);
  13. }
  14.  
  15. main(int argc, char *argv[])
  16. {
  17.     char *p;
  18.     unsigned meg;
  19.     unsigned long maxallocs;
  20.     unsigned long allocs;
  21.  
  22.     if (argc < 2)
  23.         meg = 1;        // default 1 megabyte  required 
  24.     else
  25.         meg = atoi(argv[1]);    // command-line option:  n megabytes 
  26.  
  27.     printf("Allocating %u megabyte%s...  ",meg, (meg > 1) ?  "s" :  "");
  28.  
  29.     maxallocs = ((long)meg) << 10;     // number of 1k blocks required 
  30.  
  31.     for (allocs = 0; allocs < maxallocs; allocs++)
  32.     {
  33.         if (p = malloc(1024))   // in 1k blocks 
  34.         {
  35.             *p = 'x';       // do something with the memory
  36.             p[1023] = 'y';
  37.         }
  38.         else
  39.         {
  40.             printf("Only %lu bytes available\n", allocs << 10);
  41.             fail("Insufficient memory!");
  42.         }
  43.     }
  44.  
  45.     puts("ok");
  46.     return 0;
  47.