Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Miscellaneous
complex
exception
functional
iterator
limits
locale
memory
new
numeric
stdexcept
typeinfo
utility
valarray
new
bad_alloc
new_handler
nothrow
nothrow_t
set_new_handler
global namespace:
operator delete
operator delete[]
operator new
operator new[]


bad_alloc

class
<new>
class bad_alloc;

Exception thrown on failure allocating memory

bad_alloc

Type of the exceptions thrown by the standard definitions of operator new and operator new[] when they fail to allocate the requested storage space.

This class is derived from exception. See the exception class for the member definitions of standard exceptions.

Its member what returns a null-terminated character sequence identifying the exception.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// bad_alloc example
#include <iostream>
#include <new>
using namespace std;
int main () {
  try
  {
    int* myarray= new int[10000];
  }
  catch (bad_alloc& ba)
  {
    cerr << "bad_alloc caught: " << ba.what() << endl;
  }
  return 0;
}


Possible output:

bad_alloc caught: bad allocation

See also