std::bad_array_new_length

定义于头文件 <new>
class bad_array_new_length;
(C++11 起)

std::bad_array_new_lengthnew 表达式作为异常抛出以报告非法数组长度的对象类型,若

1) 数组长度为负

2) 新数组的总大小将超过实现定义最大值

3) 初始化器子句的数量超出要初始化的元素数量

仅数组第一维可生成此异常;第一维外的维数是常量表达式,在编译时得到检查。

cpp/error/exceptioncpp/memory/new/bad allocstd-bad array new length-inheritance.svg
关于这幅图像

继承图

目录

成员函数

构造 bad_array_new_length 对象
(公开成员函数)

继承自 std::bad_alloc

继承自 std::exception

成员函数

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

注意

可提供但不要求虚成员函数 what() 的覆写。

示例

应抛出 std::bad_array_new_length 的三种条件:

#include <iostream>
#include <new>
#include <climits>
 
int main()
{
    int negative = -1;
    int small = 1;
    int large = INT_MAX;
    try {
        new int[negative];           // 负大小
        new int[small]{1,2,3};       // 过多初始化器
        new int[large][1000000];     // 过大
    } catch(const std::bad_array_new_length &e) {
        std::cout << e.what() << '\n';
    }
}


参阅

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

版本历史

  • (当前 | 先前 2017年8月24日 (四) 00:06Fruderica讨论 | 贡献. . (1,606字节) (-549). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:56P12bot讨论 | 贡献 . . (2,155字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:04P12bot讨论 | 贡献 . . (2,155字节) (-98). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:02P12bot讨论 | 贡献 . . (2,253字节) (+345). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:26P12讨论 | 贡献 . . (1,908字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,908字节) (-36). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:09P12讨论 | 贡献 . . (1,944字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,944字节) (+1,944). . (Translated from the English version using Google Translate)