共用方式為


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 回的值是實作定義的 C 字串。 所有成員函式都不會擲回任何例外狀況。

範例

// bad_alloc.cpp
// compile with: /EHsc
#include<new>
#include<iostream>
using namespace std;

int main() {
   char* ptr;
   try {
      ptr = new char[(~unsigned int((int)0)/2) - 1];
      delete[] ptr;
   }
   catch( bad_alloc &ba) {
      cout << ba.what( ) << endl;
   }
}
bad allocation