std::bad_alloc

定义于头文件 <new>
class bad_alloc;

std::bad_alloc分配函数作为异常抛出的对象类型,以报告存储分配失败。

cpp/error/exceptionstd-bad alloc-inheritance.svg
关于这幅图像

继承图

目录

成员函数

(构造函数)
构造 bad_alloc 对象
(公开成员函数)
operator=
替换一个 bad_alloc 对象
(公开成员函数)
what
返回解释性字符串
(公开成员函数)

std::bad_alloc::bad_alloc

bad_alloc();

以实现定义的空终止字节字符串构造新的 bad_alloc 对象,字符串能通过 what() 访问。

参数

(无)

异常

(无) (C++11 前)
noexcept 规定:  
noexcept
  
(C++11 起)

std::bad_alloc::operator=

bad_alloc& operator=( const bad_alloc& other );

other 的内容赋值。

参数

other - 要赋值的另一异常对象

返回值

*this

异常

(无) (C++11 前)
noexcept 规定:  
noexcept
  
(C++11 起)

std::bad_alloc::what

virtual const char* what() const;

返回解释性字符串。

参数

(无)

返回值

指向有解释性信息的空终止字符串的指针。

异常

(无) (C++11 前)
noexcept 规定:  
noexcept
  
(C++11 起)

继承自 std::exception

成员函数

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

示例

#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';
    }
}

可能的输出:

Allocation failed: std::bad_alloc

参阅

分配函数
(函数)

版本历史

  • (当前 | 先前 2017年5月17日 (三) 00:05Fruderica讨论 | 贡献. . (2,289字节) (+886). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 17:56P12bot讨论 | 贡献 . . (1,403字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 09:04P12bot讨论 | 贡献 . . (1,403字节) (-79). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 16:05P12bot讨论 | 贡献 . . (1,482字节) (+257). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月27日 (六) 03:25P12讨论 | 贡献 . . (1,225字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (1,225字节) (-30). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 14:09P12讨论 | 贡献 . . (1,255字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (1,255字节) (+1,255). . (Translated from the English version using Google Translate)