Namespaces
Variants

std::bad_alloc

ja.cppreference.net より
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型, RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変長引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20 で非推奨)
整数比較関数
(C++20)(C++20)(C++20)    
(C++20)
スワップ および 型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
共通語彙型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
メモリ管理ライブラリ
アロケータ
メモリリソース
未初期化ストレージ (C++20 まで*)
ガベージコレクタサポート (C++23 まで)
 
 
ヘッダで定義 <new>
class bad_alloc;

std::bad_alloc は、確保関数 によって記憶域の確保に失敗したことを報告するために例外として投げられるオブジェクトの型です。

cpp/error/exception

継承図

メンバー関数

(constructor)
新しい 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から)
(constexprはC++26から)
(2)
bad_alloc ( const bad_alloc & other ) throw ( ) ;
(C++11まで)
bad_alloc ( const bad_alloc & other ) noexcept ;
(C++11から)
(constexprはC++26から)

実装定義のヌル終端バイト文字列を持つ新しい bad_alloc オブジェクトを構築します。この文字列は what() を通じてアクセス可能です。

1) デフォルトコンストラクタ。
2) コピーコンストラクタ。 * this と other の両方が動的型 std::bad_alloc を持つ場合、 std:: strcmp ( what ( ) , other. what ( ) ) == 0 となります。 (C++11から)

パラメータ

other - コピーする別の例外オブジェクト

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から)

パラメータ

other - 代入する別の例外オブジェクト

戻り値

* 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 から継承

メンバ関数

例外オブジェクトを破棄
(std::exception の仮想公開メンバ関数)
[virtual]
説明文字列を返す
(std::exception の仮想公開メンバ関数)

注記

機能テスト マクロ 値 標準 機能
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr 例外型のためのconstexpr

例

#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

関連項目

確保関数
(関数)