home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c100 / 1.ddi / NEW.ZIP / NEWHAND.CPP
Encoding:
C/C++ Source or Header  |  1990-05-14  |  1.0 KB  |  41 lines

  1. //  You can place a handler on the free store allocation to globally
  2. //  detect if the new operator correctly allocated memory.
  3. //  NOTE:  This is an old C++ 1.2 feature that we ported forward
  4. //         to support old code.  I would not recommend building a
  5. //         large program around this feature as it is NOT
  6. //         part of the C++ 2.0 specs.
  7.  
  8. #include <iostream.h>
  9. #include <string.h>
  10. #include <process.h>
  11.  
  12. //  Declare the external new handler
  13. extern void (*_new_handler)();
  14.  
  15. //  Write the function to handler exhausted heap
  16. void heap_exhausted( void )
  17. {
  18.    cout << "No remaining heap!\n";
  19.    exit(1);
  20. }
  21.  
  22. //  Install the new handler
  23. //  I have used a new TURBO C++ pragma to force the setup to happen
  24. //  before main.
  25. void setup( void )
  26. {
  27.    _new_handler = heap_exhausted;
  28. }
  29. #pragma startup setup 70
  30.  
  31. //  Here is a main that will exhaust the free store in the
  32. //  small memory model.
  33. main()
  34. {
  35.    char *one = new char[60000u];
  36.    char *two = new char[60000u];
  37.    strcmp(one, two);
  38.  
  39.    return 0;
  40. }
  41.