17.8. Processing new Failures

When operator new fails, it throws a bad_alloc exception (defined in header <new>). In this section, we present two examples of new failing. The first uses the version of new that throws a bad_alloc exception when new fails. The second uses function set_new_handler to handle new failures. [Note: The examples in Figs. 17.517.6 allocate large amounts of dynamic memory, which could cause your computer to become sluggish.]


 1   // Fig. 17.5: fig17_05.cpp
 2   // Demonstrating standard new throwing bad_alloc when memory
 3   // cannot be allocated.
 4   #include <iostream>
 5   #include <new> // bad_alloc class is defined here
 6   using namespace std;
 7
 8   int main()
 9   {
10      double *ptr[ 50 ];
11
12      // aim each ptr[i] at a big block of memory
13      try
14      {
15         // allocate memory for ptr[ i ]; new throws bad_alloc on failure
16         for ( size_t i = 0; i < 50; ++i )
17         {
18            ptr[ i ] = new double[ 50000000 ]; // may throw exception
19            cout << "ptr[" << i << "] points to 50,000,000 new doubles ";
20         } // end for
21      } // end try
22      catch ( bad_alloc &memoryAllocationException )
23      {
24         cerr << "Exception occurred: "
25            << memoryAllocationException.what() << endl;
26      } // end catch
27   } // end main


ptr[0] points to 50,000,000 new doubles
ptr[1] points to 50,000,000 new doubles
ptr[2] points to 50,000,000 new doubles
ptr[3] points to 50,000,000 new doubles
Exception occurred: bad allocation


Fig. 17.5. new throwing bad_alloc on failure.


 1   // Fig. 17.6: fig17_06.cpp
 2   // Demonstrating set_new_handler.
 3   #include <iostream>
 4   #include <new> // set_new_handler function prototype
 5   #include <cstdlib> // abort function prototype
 6   using namespace std;
 7
 8   // handle memory allocation failure      
 9   void customNewHandler()                  
10   {                                        
11      cerr << "customNewHandler was called";
12      abort();                              
13   } // end function customNewHandler       
14
15   // using set_new_handler to handle failed memory allocation
16   int main()
17   {
18      double *ptr[ 50 ];
19
20      // specify that customNewHandler should be called on
21      // memory allocation failure                        
22      set_new_handler( customNewHandler );                
23
24      // aim each ptr[i] at a big block of memory; customNewHandler will be
25      // called on failed memory allocation
26      for ( size_t i = 0; i < 50; ++i )
27      {
28         ptr[ i ] = new double[ 50000000 ]; // may throw exception
29         cout << "ptr[" << i << "] points to 50,000,000 new doubles ";
30      } // end for
31   }  // end main


ptr[0] points to 50,000,000 new doubles
ptr[1] points to 50,000,000 new doubles
ptr[2] points to 50,000,000 new doubles
ptr[3] points to 50,000,000 new doubles
customNewHandler was called


Fig. 17.6. set_new_handler specifying the function to call when new fails.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset