Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/new/bad alloc"

From cppreference.com
< cpp‎ | memory‎ | new
(Example: fmt)
m (Text replace - "{{example cpp" to "{{example")
Line 20: Line 20:
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
 
  |
 
  |
 
  | code=
 
  | code=

Revision as of 16:57, 19 April 2012

Template:cpp/memory/new/bad alloc/sidebar Template:ddcl list begin <tr class="t-dsc-header">

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

<td></td> <td></td> </tr> <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.

Member functions

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

Template:cpp/error/exception/exception/inherit

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