home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX0704I.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  2.5 KB  |  67 lines

  1. // Demonstration of user defined _new_handler
  2.  
  3. // Files used in this example
  4. // \EXAMPLES\EX0704.CPP -- this file
  5.  
  6. #include <iostream.h>             // needed for cout
  7. #include <new.h>                  // needed for _new_handler
  8. #include <process.h>              // needed for exit(int)
  9.  
  10. //-------------------------------------------------------------
  11. // prototype of _new_handler function
  12. //-------------------------------------------------------------
  13. // in <new.h>:  extern void (*set_new_handler (void (*)())) ()
  14. //         BORLAND TURBO C++
  15. // void my_new_handler();
  16. //         IBM C SET II
  17.    void my_new_handler();
  18. //         MICROSOFT VIRTUAL C++
  19. // int my_new_handler(size_t);
  20.  
  21. //-------------------------------------------------------------
  22. // allocate blocks of 10,000 integers until memory is exhausted
  23. //-------------------------------------------------------------
  24.  
  25. void main()
  26. {  int c = 0;                       // to count allocations
  27. //
  28. // specify the new handler function
  29. //         BORLAND TURBO C++
  30. //   _new_handler = my_new_handler;
  31. // set_new_handler( my_new_handler);
  32. //         IBM C SET II
  33.    set_new_handler(&my_new_handler );
  34. //         MICROSOFT VISUAL C++
  35. // _set_new_handler( my_new_handler );
  36. //
  37.    while ( c < 100 )              // up to 10,000 allocations
  38.    {  int *ip = new int[10000];       // of 10,000 integers
  39.       if ( ip == 0 )               // should never execute
  40.       { cout << "  Allocation failed"   // failure caught by
  41.              << endl;                 // _new_handler
  42.         break;                        // jump out of loop
  43.       };                              //
  44.       cout << "allocated bytes:  " // output total allocated
  45.            << ( ++c * 10000 * sizeof(int) )  // number of bytes
  46.            << endl;                          //
  47.    };                              // end of loop
  48.    cout << "is Memory inifinite?"  // allocations complete
  49.         << endl;                            // 100,000,000 ints
  50. }                                  // end of program
  51.  
  52. //-------------------------------------------------------------
  53. // definition of new handler function
  54. //-------------------------------------------------------------
  55. //         BORLAND TURBO C++
  56. // void my_new_handler()
  57. //         IBM C SET II
  58.    void my_new_handler()
  59. //         MICROSOFT VIRTUAL C++
  60. // int my_new_handler (size_t i)
  61. {  cout << "My new handler called"    // print message
  62.         << endl;
  63.    exit(1);                // terminate - return code 1
  64. //         MICROSOFT VIRTUAL C++
  65. // return 1;
  66. }
  67.