Skip to content

Latest commit

 

History

History
41 lines (34 loc) · 814 Bytes

bad_alloc.md

File metadata and controls

41 lines (34 loc) · 814 Bytes

bad_alloc

  • new[meta header]
  • std[meta namespace]
  • class[meta id-type]
namespace std {
  class bad_alloc : public exception {
  public:
    bad_alloc() noexcept;
    bad_alloc(const bad_alloc&) noexcept;
    bad_alloc& operator=(const bad_alloc&) noexcept;
    virtual const char* what() const noexcept;
  };
}
  • exception[link /reference/exception/exception.md]

概要

何らかの理由で記憶域の動的確保に失敗するなど、get_new_handler()nullptrを返した場合に投げられる例外。

#include <iostream>
#include <new>

struct X {};

int main()
{
  try {
    X* x = new X();
  }
  catch (std::bad_alloc& e) {
    // メモリ確保に失敗
    std::cout << e.what() << std::endl;
  }
}
  • std::bad_alloc[color ff0000]