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

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