home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l351 / 1.ddi / EXAMPLES / MEMTEST2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-26  |  1.2 KB  |  54 lines

  1. /* 
  2. MEMTEST2.CPP
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <iostream.h>
  7. #include <new.h>
  8. #include <string.h>
  9.  
  10. class msg {
  11. public:
  12.     msg()   { cout << "hello from " << __FILE__ << "\n" ; }
  13.     ~msg()  { cout << "bye\n" ; }
  14.     } ;
  15.  
  16. static msg banner;  // test C++ static constructors, destructors
  17.  
  18. static unsigned long bytes = 0;
  19. static unsigned long allocs = 0;
  20. static unsigned blk_size = 10240;
  21.  
  22. int new_fail(size_t )
  23. {
  24.     if (blk_size)
  25.         blk_size >>= 1;  // try to allocate a smaller block
  26.     else
  27.     {   // memory exhausted
  28.         cout << "Allocated " << bytes << " bytes\n" ;
  29.         exit(1);
  30.     }
  31.     return 1;
  32. }
  33.  
  34. void
  35. main(void)
  36. {
  37.     char *p;
  38.  
  39.     _set_new_handler(new_fail);   // called when new fails
  40.  
  41.     for (;;)
  42.     {
  43.        p = new char[blk_size];   // allocate memory
  44.        memset(p, 0, blk_size);   // touch every byte
  45.        *p = 'x';                 // do something, anything with
  46.        p[blk_size-1] = 'y';      //   the allocated memory
  47.            
  48.        bytes += blk_size;
  49.        allocs++;
  50.        if ((allocs % 25) == 0)   // odometer
  51.             cout << "Allocated " << bytes << " bytes\r";
  52.     }
  53. }
  54.