当分配内存的请求没有被成功执行时,系统会抛出一个用bad_alloc类来描述出的异常。
1 定义
bad_alloc类的定义如下所示
class bad_alloc : public exception
{
bad_alloc();
virtual ~bad_alloc();
bad_alloc(const bad_alloc&);
bad_alloc& operator=(const bad_alloc&);
const char* what() const override;
};
其中,what()成员函数返回的是分配内存失败的原因。
2 例子
#include<iostream>
using namespace std;
int main()
{
char* ptr;
try {
ptr = new char[(~unsigned int((int)0) / 2) -
订阅专栏 解锁全文
1万+

被折叠的 条评论
为什么被折叠?



