Demonstrating new throwing bad_alloc when memory is not allocated : new delete « Development « C++






Demonstrating new throwing bad_alloc when memory is not allocated

   
#include <iostream>

using std::cout;
using std::endl;

#include <new>

using std::bad_alloc;

int main()
{
   double *ptr[ 50 ];
   
   try {   
      for ( int i = 0; i < 50; i++ ) {
         ptr[ i ] = new double[ 5000000 ];
         cout << "Allocated 5000000 doubles in ptr[ " << i << " ]\n";
      }
   }
   catch ( bad_alloc exception ) {
      cout << "Exception occurred: " 
           << exception.what() << endl;
   }
   
   return 0;
}
  
    
    
  








Related examples in the same category

1.delete memory allocate for objects
2.Use new operator to allocate memory for object
3.bad_alloc standard exception