Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory‎ | new
m (Update links.)
(inline members)
Line 1: Line 1:
 
{{cpp/title|bad_alloc}}
 
{{cpp/title|bad_alloc}}
{{cpp/memory/new/bad_alloc/navbar}}
+
{{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/operator{{=}} | replaces a bad_alloc object}}
+
{{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/what | returns explanatory string}}
+
{{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.

cpp/error/exceptionstd-bad alloc-inheritance.svg

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) [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]