home *** CD-ROM | disk | FTP | other *** search
- // Demonstration of user defined _new_handler
-
- // Files used in this example
- // \EXAMPLES\EX0704.CPP -- this file
-
- #include <iostream.h> // needed for cout
- #include <new.h> // needed for _new_handler
- #include <process.h> // needed for exit(int)
-
- //-------------------------------------------------------------
- // prototype of _new_handler function
- //-------------------------------------------------------------
- // in <new.h>: extern void (*set_new_handler (void (*)())) ()
- // BORLAND TURBO C++
- // void my_new_handler();
- // IBM C SET II
- void my_new_handler();
- // MICROSOFT VIRTUAL C++
- // int my_new_handler(size_t);
-
- //-------------------------------------------------------------
- // allocate blocks of 10,000 integers until memory is exhausted
- //-------------------------------------------------------------
-
- void main()
- { int c = 0; // to count allocations
- //
- // specify the new handler function
- // BORLAND TURBO C++
- // _new_handler = my_new_handler;
- // set_new_handler( my_new_handler);
- // IBM C SET II
- set_new_handler(&my_new_handler );
- // MICROSOFT VISUAL C++
- // _set_new_handler( my_new_handler );
- //
- while ( c < 100 ) // up to 10,000 allocations
- { int *ip = new int[10000]; // of 10,000 integers
- if ( ip == 0 ) // should never execute
- { cout << " Allocation failed" // failure caught by
- << endl; // _new_handler
- break; // jump out of loop
- }; //
- cout << "allocated bytes: " // output total allocated
- << ( ++c * 10000 * sizeof(int) ) // number of bytes
- << endl; //
- }; // end of loop
- cout << "is Memory inifinite?" // allocations complete
- << endl; // 100,000,000 ints
- } // end of program
-
- //-------------------------------------------------------------
- // definition of new handler function
- //-------------------------------------------------------------
- // BORLAND TURBO C++
- // void my_new_handler()
- // IBM C SET II
- void my_new_handler()
- // MICROSOFT VIRTUAL C++
- // int my_new_handler (size_t i)
- { cout << "My new handler called" // print message
- << endl;
- exit(1); // terminate - return code 1
- // MICROSOFT VIRTUAL C++
- // return 1;
- }
-