Namespaces
Variants

std:: bad_array_new_length

From cppreference.net
< cpp ‎ | memory ‎ | new
Utilities library
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
ヘッダーで定義 <new>
class bad_array_new_length : std:: bad_alloc
(C++11以降)

std::bad_array_new_length は、 new式 が無効な配列長を報告する際に例外としてスローするオブジェクトの型です。条件は以下の通りです:

  1. 配列の長さが負である、
  2. 新しい配列の合計サイズが実装定義の最大値を超える、
  3. 初期化子の数が初期化する要素の数を超える。

最初の配列次元のみがこの例外を生成する可能性があります。最初以外の次元は定数式であり、コンパイル時にチェックされます。

cpp/error/exception cpp/memory/new/bad alloc std-bad array new length-inheritance.svg

継承図

目次

メンバー関数

(constructor)
新しい bad_array_new_length オブジェクトを構築する
(public member function)
operator=
bad_array_new_length オブジェクトを置き換える
(public member function)
what
説明文字列を返す
(public member function)

std::bad_array_new_length:: bad_array_new_length

bad_array_new_length ( ) noexcept ;
(1) (C++11以降)
(constexpr since C++26)
bad_array_new_length ( const bad_array_new_length & other ) noexcept ;
(2) (C++11以降)
(constexpr since C++26)

新しい bad_array_new_length オブジェクトを構築します。実装定義のnull終端バイト文字列が含まれ、これは what() を通じてアクセス可能です。

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

パラメータ

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

std::bad_array_new_length:: operator=

bad_array_new_length & operator = ( const bad_array_new_length & other ) noexcept ;
(C++11以降)
(constexpr C++26以降)

other の内容を代入します。 * this other の両方が動的型 std::bad_array_new_length を持つ場合、代入後は std:: strcmp ( what ( ) , other. what ( ) ) == 0 となります。

パラメータ

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

戻り値

* this

std::bad_array_new_length:: what

virtual const char * what ( ) const noexcept ;
(C++11以降)
(constexpr C++26以降)

説明文字列を返します。

戻り値

説明情報を含む実装定義のナル終端文字列へのポインタ。この文字列は std::wstring への変換と表示に適しています。ポインタは、少なくとも取得元の例外オブジェクトが破棄されるまで、または例外オブジェクトの非constメンバ関数(コピー代入演算子など)が呼び出されるまで有効であることが保証されます。

定数評価中、返される文字列は通常のリテラルエンコーディングでエンコードされます。

(C++26以降)

注記

実装は what() をオーバーライドすることが許可されていますが、必須ではありません。

std:: bad_alloc から継承

std::exception から継承 std:: exception

メンバ関数

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

注記

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

std::bad_array_new_length がスローされるべき3つの条件:

#include <climits>
#include <iostream>
#include <new>
int main()
{
    try
    {
        int negative = -1;
        new int[negative];
    }
    catch (const std::bad_array_new_length& e)
    {
        std::cout << "1) " << e.what() << ": negative size\n";
    }
    try
    {
        int small = 1;
        new int[small]{1,2,3};
    }
    catch (const std::bad_array_new_length& e)
    {
        std::cout << "2) " << e.what() << ": too many initializers\n";
    }
    try
    {
        long large = LONG_MAX;
        new int[large][1000];
    } 
    catch (const std::bad_array_new_length& e)
    {
        std::cout << "3) " << e.what() << ": too large\n";
    }
    std::cout << "End\n";
}

出力例:

1) std::bad_array_new_length: negative size
2) std::bad_array_new_length: too many initializers
3) std::bad_array_new_length: too large
End

関連項目

メモリ確保関数
(関数)
メモリ確保が失敗したときにスローされる例外
(クラス)