名前空間
変種
操作

std::bad_alloc

提供: cppreference.com
< cpp‎ | memory‎ | new
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
動的メモリ管理
スマートポインタ
(C++11)
(C++11)
(C++11)
(C++17未満)
(C++11)
アロケータ
メモリリソース
未初期化記憶域
ガベージコレクションサポート
その他
(C++20)
(C++11)
(C++11)
C のライブラリ
低水準のメモリ管理
 
 
ヘッダ <new> で定義
class bad_alloc;

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

cpp/error/exceptionstd-bad alloc-inheritance.svg
画像の詳細

継承図

目次

[編集] メンバ関数

コンストラクタ
bad_alloc オブジェクトを構築します
(パブリックメンバ関数)
operator=
bad_alloc オブジェクトを置き換えます
(パブリックメンバ関数)
what
説明文字列を返します
(パブリックメンバ関数)

std::bad_alloc::bad_alloc

bad_alloc() throw();
(C++11未満)
bad_alloc() noexcept;
(C++11以上)

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

引数

(なし)

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 の内容を代入します。

引数

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

戻り値

*this

std::bad_alloc::what

virtual const char* what() const throw();
(C++11未満)
virtual const char* what() const noexcept;
(C++11以上)

説明文字列を返します。

引数

(なし)

戻り値

説明情報を持つヌル終端文字列へのポインタ。

std::exception から継承

メンバ関数

例外オブジェクトを破棄します
(std::exceptionの仮想パブリックメンバ関数) [edit]
[仮想]
説明文字列を返します
(std::exceptionの仮想パブリックメンバ関数) [edit]

[編集]

#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

[編集] 関連項目

確保関数
(関数) [edit]