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

std::bad_alloc

Defined in header <new>

class bad_alloc;

std::bad_alloc作为异常抛出的对象的类型。分配函数若要报告分配存储空间失败,请执行以下操作。

二次

二次

继承图

成员函数

(constructor)

constructs the bad_alloc object (public member function)

operator=

replaces a bad_alloc object (public member function)

what

returns explanatory string (public member function)

STD:不好[医]阿罗克::坏[医]异种

bad_alloc();

构造新bad_alloc对象,该对象具有实现定义的以空结尾的字节字符串,该字符串可通过以下方式访问:what()...

参数

%280%29

例外

(none)

(until C++11)

noexcept specification: noexcept

(since C++11)

STD:不好[医]分配数::操作员=

bad_alloc& operator=( const bad_alloc& other );

分配...的内容other...

参数

other

-

another exception object to assign

返回值

*this...

例外

(none)

(until C++11)

noexcept specification: noexcept

(since C++11)

STD:不好[医]阿罗克:什么?

virtual const char* what() const;

返回解释性字符串。

参数

%280%29

返回值

指向带解释性信息的以空结尾的字符串的指针。

例外

(none)

(until C++11)

noexcept specification: noexcept

(since C++11)

继承自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)

二次

代码语言:javascript
复制
#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';
    }
}

二次

可能的产出:

二次

代码语言:javascript
复制
Allocation failed: std::bad_alloc

二次

另见

operator newoperator new[]

allocation functions (function)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券