Listing 17 Illustrates set_new_handler

// exhaust1.cpp
#include <iostream.h>
#include <stdlib.h>
#include <new.h>

inline void my_handler()
{
   cout << "Memory exhausted" << endl;
   abort();
}

main()
{
   set_new_handler(my_handler);

   for (int i = 0; ; ++i)
   {
      (void) new double[100];
      if ((i+1)%10 == 0)
         cout << (i+1) << " allocations" << endl;
   }
}

/* Output:
10 allocations
20 allocations
30 allocations
40 allocations
50 allocations
60 allocations
70 allocations
Memory exhausted
Abnormal program termination
*/

// End of File