当不使用operator new时,C ++程序在std :: bad_alloc之后终止

时间:2018-02-28 07:15:06

标签: c++11 boost

根据cplusplus.com对std :: bad_alloc

的说法
  

当operator new和operator new []的标准定义未能分配所请求的存储空间时抛出的异常类型。

但是,在我的代码中,不使用new运算符:

#include <iostream>
#include <boost/filesystem.hpp>
#include <cstdint>

using namespace std;
using namespace boost::filesystem;

int main()
{
  path p{};

  std::cin >> p;

  if (exists(p))    // does p actually exist?
  {
    if (is_regular_file(p))        // is p a regular file?
      cout << p << " size is " << file_size(p) << '\n';

    else if (is_directory(p))      // is p a directory?
      cout << p << "is a directory\n";

    else
      cout << p << "exists, but is neither a regular file nor a directory\n";
  }
  else
    cout << p << "does not exist\n";

  return 0;
}

(来自boost文件系统教程的代码)

然而控制台说:

terminate called after throwing an instance of 'std::bad_alloc'
  what(): std::bad_alloc
Aborted (core dumped)

0 个答案:

没有答案