set_new_handler 用法

 

用法小demo,定义参见crt目录下的new头文件源码

#include <new>
#include <cstddef>
#include <cstdlib>
#include <climits>
#include <iostream>

using namespace std;
void __cdecl newhandler()
{

    cout << "The new_handler is called:" << endl;

    throw bad_alloc();

    return;

}

int main()
{

    set_new_handler (newhandler);
    try
    {

        while (1)
        {

            new int[5000000];

            cout << "Allocating 5000000 ints." << endl;
        }
    }

    catch ( exception e )
    {
        cout << e.what() << " xxx" << endl;
    }

    return 0;
}

 

posted @ 2013-11-15 16:31  calabashdad  阅读(515)  评论(0)    收藏  举报