首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::bad_array_new_length

Defined in header <new>

class bad_array_new_length;

(since C++11)

std::bad_array_new_length作为异常抛出的对象的类型。新表达式若要报告无效的数组长度,请执行以下操作。

1%29数组长度为负值。

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

3%29初始化器-子句的数量超过要初始化的元素数。

只有第一个数组维度可以生成此异常;第一个维度以外的维度是常量表达式,在编译时进行检查。

二次

二次

继承图

成员函数

(constructor)

constructs the bad_array_new_length object (public member function)

继承自STD:不好[医]异种

继承自STD:例外

成员函数

(destructor) virtual

destructs the exception object (virtual public member function of std::exception)

what virtual

returns an explanatory string (virtual public member function of std::exception)

注记

虚拟成员函数的覆盖。what()可由提供,但不是必需的。

三个条件std::bad_array_new_length应投:

二次

代码语言:javascript
复制
#include <iostream>
#include <new>
#include <climits>
 
int main()
{
    int negative = -1;
    int small = 1;
    int large = INT_MAX;
    try {
        new int[negative];           // negative size
        new int[small]{1,2,3};       // too many initializers
        new int[large][1000000];     // too large
    } catch(const std::bad_array_new_length &e) {
        std::cout << e.what() << '\n';
    }
}

二次

另见

operator newoperator new[]

allocation functions (function)

bad_alloc

exception thrown when memory allocation fails (class)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券