home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l352 / 1.img / EXAMPLES / MEMTEST2.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-27  |  1.1 KB  |  53 lines

  1. /* 
  2. MEMTEST2.CPP
  3. bcc286 memtest2.cpp
  4. */
  5.  
  6. #include <stdlib.h>
  7. #include <iostream.h>
  8. #include <new.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. void new_fail(void)
  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. }
  32.  
  33. main()
  34. {
  35.     char *p;
  36.  
  37.     set_new_handler(new_fail);   // called when new fails
  38.  
  39.     for (;;)
  40.     {
  41.        p = new char[blk_size];   // allocate memory
  42.        memset(p, 0, blk_size);   // touch every byte
  43.        *p = 'x';                 // do something, anything with
  44.        p[blk_size-1] = 'y';      //   the allocated memory
  45.            
  46.        bytes += blk_size;
  47.        allocs++;
  48.        if ((allocs % 25) == 0)   // odometer
  49.             cout << "Allocated " << bytes << " bytes\r";
  50.     }
  51. }
  52.  
  53.