Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory‎ | new
m (Shorten template names. Use {{lc}} where appropriate.)
m (Update links.)
Line 44: Line 44:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/memory/new/dcl list operator_new}}
+
{{dsc inc | cpp/memory/new/dsc operator_new}}
 
{{dsc end}}
 
{{dsc end}}
  

Revision as of 22:12, 31 May 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
 
std::bad_alloc
 
Defined in header <new>
class bad_alloc : public std::exception;

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

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]
destroys 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

allocation functions
(function) [edit]