Difference between revisions of "cpp/memory/new/bad alloc"
From cppreference.com
m (Update links.) |
(inline members) |
||
Line 1: | Line 1: | ||
{{cpp/title|bad_alloc}} | {{cpp/title|bad_alloc}} | ||
− | {{cpp/memory/new/bad_alloc | + | {{cpp/memory/new/bad_alloc}} |
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header | new}} | {{dcl header | new}} | ||
Line 14: | Line 14: | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc mem ctor | cpp/memory/new/bad_alloc/bad_alloc | constructs the bad_alloc object}} | + | {{dsc mem ctor | cpp/memory/new/bad_alloc/bad_alloc | inlinemem=true | constructs the bad_alloc object}} |
− | {{dsc mem fun | cpp/memory/new/bad_alloc | + | {{dsc mem fun | cpp/memory/new/bad_alloc | title=operator{{=}} | inlinemem=true | replaces a bad_alloc object}} |
− | {{dsc mem fun | cpp/memory/new/bad_alloc | + | {{dsc mem fun | cpp/memory/new/bad_alloc | title=what | inlinemem=true | returns explanatory string}} |
{{dsc end}} | {{dsc end}} | ||
+ | |||
+ | {{member | {{small|std::bad_alloc::}}bad_alloc | | ||
+ | {{ddcl | | ||
+ | bad_alloc(); | ||
+ | }} | ||
+ | |||
+ | Constructs new {{tt|bad_alloc}} object with an implementation-defined null-terminated byte string which is accessible through {{ltf|cpp/error/exception/what}}. | ||
+ | |||
+ | ===Parameters=== | ||
+ | (none) | ||
+ | |||
+ | ===Exceptions=== | ||
+ | {{rev begin}} | ||
+ | {{rev | since=c++11 | {{noexcept}} }} | ||
+ | {{rev end}} | ||
+ | }} | ||
+ | |||
+ | {{member | {{small|std::bad_alloc::}}operator{{=}} | | ||
+ | {{dcl begin}} | ||
+ | {{dcl | 1= | ||
+ | bad_alloc& operator=( const bad_alloc& other ); | ||
+ | }} | ||
+ | {{dcl end}} | ||
+ | |||
+ | Assigns the contents of {{tt|other}}. | ||
+ | |||
+ | ===Parameters=== | ||
+ | {{par begin}} | ||
+ | {{par | other | another exception object to assign}} | ||
+ | {{par end}} | ||
+ | |||
+ | ===Return value=== | ||
+ | {{c|*this}} | ||
+ | |||
+ | ===Exceptions=== | ||
+ | {{rev begin}} | ||
+ | {{rev | since=c++11 | {{noexcept}} }} | ||
+ | {{rev end}} | ||
+ | }} | ||
+ | |||
+ | {{member | {{small|std::bad_alloc::}}what | | ||
+ | {{dcl begin}} | ||
+ | {{dcl | | ||
+ | virtual const char* what() const; | ||
+ | }} | ||
+ | {{dcl end}} | ||
+ | |||
+ | Returns the explanatory string. | ||
+ | |||
+ | ===Parameters=== | ||
+ | (none) | ||
+ | |||
+ | ===Return value=== | ||
+ | Pointer to a null-terminated string with explanatory information. | ||
+ | |||
+ | ===Exceptions=== | ||
+ | {{rev begin}} | ||
+ | {{rev | since=c++11 | {{noexcept}} }} | ||
+ | {{rev end}} | ||
+ | }} | ||
{{cpp/error/exception/inherit}} | {{cpp/error/exception/inherit}} |
Revision as of 22:52, 5 June 2013
Template:cpp/memory/new/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.
Inheritance diagram
Contents |
Member functions
constructs the bad_alloc object (public member function) | |
operator= |
replaces a bad_alloc object (public member function) |
what |
returns explanatory string (public member function) |
std::bad_alloc::bad_alloc
bad_alloc(); |
||
Constructs new bad_alloc
object with an implementation-defined null-terminated byte string which is accessible through what().
Parameters
(none)
Exceptions
noexcept specification: noexcept |
(since C++11) |
std::bad_alloc::operator=
bad_alloc& operator=( const bad_alloc& other ); |
||
Assigns the contents of other
.
Parameters
other | - | another exception object to assign |
Return value
*this
Exceptions
noexcept specification: noexcept |
(since C++11) |
std::bad_alloc::what
virtual const char* what() const; |
||
Returns the explanatory string.
Parameters
(none)
Return value
Pointer to a null-terminated string with explanatory information.
Exceptions
noexcept specification: noexcept |
(since C++11) |
Inherited from std::exception
Member functions
[virtual] |
destroys the exception object (virtual public member function of std::exception )
|
[virtual] |
returns an explanatory string (virtual public member function of std::exception )
|
Example
Run this code
#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) |