std::bad_alloc
From cppreference.com
| ヘッダ <new> で定義 |
||
| class bad_alloc; |
||
std::bad_alloc は、割り当て関数がストレージの割り当て失敗を報告するために例外としてスローするオブジェクトの型です。
継承図
目次 |
[編集] メンバー関数
| (コンストラクタ) |
新しい bad_alloc オブジェクトを構築する(public member function) |
| operator= |
bad_alloc オブジェクトを置き換える(public member function) |
| what |
説明文字列を返す (public member function) |
std::bad_alloc::bad_alloc
| (1) | ||
bad_alloc() throw(); |
(C++11まで) | |
| bad_alloc() noexcept; |
(C++11以降) | |
| (2) | ||
bad_alloc( const bad_alloc& other ) throw(); |
(C++11まで) | |
| bad_alloc( const bad_alloc& other ) noexcept; |
(C++11以降) | |
what() を介してアクセス可能な、実装定義のヌル終端バイト文字列を持つ新しい bad_alloc オブジェクトを構築します。
1) デフォルトコンストラクタ。
2) コピーコンストラクタ。*this と other の両方が動的な型
std::bad_alloc を持つ場合、std::strcmp(what(), other.what()) == 0。(C++11以降)パラメータ
| その他 | - | コピーする別の例外オブジェクト |
std::bad_alloc::operator=
| bad_alloc& operator=( const bad_alloc& other ) throw(); |
(C++11まで) | |
| bad_alloc& operator=( const bad_alloc& other ) noexcept; |
(C++11以降) | |
other の内容を割り当てます。*this と other の両方が動的な型 std::bad_alloc を持つ場合、割り当て後に std::strcmp(what(), other.what()) == 0。(C++11以降)
パラメータ
| その他 | - | 割り当てる別の例外オブジェクト |
戻り値
*this
std::bad_alloc::what
| virtual const char* what() const throw(); |
(C++11まで) | |
virtual const char* what() const noexcept; |
(C++11以降) (C++26 以降 constexpr) |
|
説明文字列を返します。
戻り値
説明情報を含む、実装定義のヌル終端文字列へのポインタ。この文字列は std::wstring として変換および表示するのに適しています。このポインタは、それが取得された例外オブジェクトが破棄されるか、例外オブジェクトの非 const メンバー関数(例:コピー代入演算子)が呼び出されるまで、少なくとも有効であることが保証されます。
|
返された文字列は、定数評価中に通常のリテラルエンコーディングでエンコードされます。 |
(C++26以降) |
注釈
実装は what() をオーバーライドすることが許可されていますが、必須ではありません。
std::exception から継承
メンバ関数
| [virtual] |
例外オブジェクトを破棄する ( std::exception の仮想 public メンバー関数) |
| [virtual] |
説明文字列を返す ( std::exception の仮想 public メンバー関数) |
[編集] 備考
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_constexpr_exceptions |
202411L |
(C++26) | constexpr for exception types |
[編集] 例
このコードを実行
#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'; } }
実行結果の例
Allocation failed: std::bad_alloc
[編集] 関連項目
| メモリ割り当て関数 (関数) |