Effective C++ Chapter8-定制new和del

2017-04-21  本文已影响0人  寒冰豌豆
49、了解new-handler的行为
1.png
#include <midstream>
#include <new>
 
void handler()
{
    std::cout << "Memory allocation failed, terminating\n";
    std::set_new_handler(nullptr);//注释掉这句,在运行试试!
}
 
int main()
{
    std::set_new_handler(handler);
    try {
        while (true) {
            new int[100000000ul];
        }
    } catch (const std::bad_alloc& e) {
        std::cout << e.what() << '\n';
    }
}
上一篇 下一篇

猜你喜欢

热点阅读