bad_alloc Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at bad_alloc Class.

The class describes an exception thrown to indicate that an allocation request did not succeed.

class bad_alloc : public exception {  
    bad_alloc();
virtual ~bad_alloc();

};  

The value returned by what is an implementation-defined C string. None of the member functions throw any exceptions.

Header: <new>

Namespace: std

// bad_alloc.cpp  
// compile with: /EHsc  
#include<new>  
#include<iostream>  
using namespace std;  
  
int main() {  
   char* ptr;  
   try {  
      ptr = new char[(~unsigned int((int)0)/2) - 1];  
      delete[] ptr;  
   }  
   catch( bad_alloc &ba) {  
      cout << ba.what( ) << endl;  
   }  
}  

bad allocation  

Header: <new>

exception Class
Thread Safety in the C++ Standard Library

Show: