C++ 新庫 - bad_alloc



描述

此異常在記憶體分配失敗時丟擲,以及標準operator new和operator new[]定義在無法分配請求的儲存空間時丟擲的異常型別。

以下是std::bad_alloc的宣告。

		
class bad_alloc;

引數

返回值

異常

不丟擲保證 - 此成員函式從不丟擲異常。

資料競爭

示例

以下示例演示std::bad_alloc。

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

輸出應如下所示:

It will throw an exception error
new.htm
廣告
© . All rights reserved.