Namespaces
Variants
Views
Actions

std::bad_alloc

From cppreference.com
< cpp‎ | memory‎ | new
Revision as of 02:33, 2 April 2013 by 217.70.176.166 (Talk)

 
 
 
 
 
std::bad_alloc
 

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <new>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
class bad_alloc : public std::exception;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

std::bad_alloc is the type of the object thrown as exceptions by the allocation functions to report failure to allocate storage.

cpp/error/exceptionstd-bad alloc-inheritance.svg
About this image

Inheritance diagram

Contents

Member functions

constructs the bad_alloc object
(public member function)
replaces a bad_alloc object
(public member function)
returns explanatory string
(public member function)

Inherited from std::exception

Member functions

[virtual]
destructs the exception object
(virtual public member function of std::exception) [edit]
[virtual]
returns an explanatory string
(virtual public member function of std::exception) [edit]

Example

#include <iostream>
#include <new>
 
int main()
{
    try {
        while (true) {
            new int[100000000ul];
        }
    } catch (const std::bad_alloc& e) {
        std::cout << "Allocation failed: " << e.what() << '\n';
    }
}

Output:

Allocation failed: std::bad_alloc

See also

Template:cpp/memory/new/dcl list operator new