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 对象
(公开成员函数)
operator=
替换 bad_array_new_length 对象
(公开成员函数)
what
返回说明性字符串
(公开成员函数)

std::bad_array_new_length:: bad_array_new_length

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

构造一个新的 bad_array_new_length 对象,该对象包含一个实现定义的以空字符结尾的字节字符串,可通过 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 ;
(since C++11)
(constexpr since C++26)

返回解释性字符串。

返回值

指向实现定义的以空字符结尾的字符串的指针,该字符串包含解释性信息。该字符串适合转换并显示为 std::wstring 。该指针保证至少在获取它的异常对象被销毁之前,或者在异常对象上调用非const成员函数(例如拷贝赋值运算符)之前保持有效。

在常量求值期间,返回的字符串使用普通字面量编码进行编码。

(since C++26)

注解

实现允许但不要求重写 what()

继承自 std:: exception

成员函数

[virtual]
销毁异常对象
( std::exception 的虚公开成员函数)
[virtual]
返回解释性字符串
( std::exception 的虚公开成员函数)

注释

功能测试 标准 功能
__cpp_lib_constexpr_exceptions 202411L (C++26) constexpr for exception types

示例

应当抛出 std::bad_array_new_length 的三种情况:

#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

参见

分配函数
(函数)
内存分配失败时抛出的异常
(类)