#include #include #include //for nothrow, bad_alloc, set_new_handler using namespace std; const char *progname; void my_new_handler(); int main(int argc, char **argv) { progname = argv[0]; cout << "How many bytes do you want to allocate? "; size_t n; cin >> n; char *const p1 = new(nothrow) char [n]; //return 0 on error if (p1 == 0) { cerr << progname << ": out of store\n"; return EXIT_FAILURE; } delete[] p1; try { char *const p2 = new char [n]; //throw exception on error delete[] p2; } catch (const bad_alloc& bad) { cerr << progname << ": out of store: " << bad.what() << "\n"; return EXIT_FAILURE; } set_new_handler(my_new_handler); char *const p3 = new char [n]; //call my_new_handler on error delete[] p3; return EXIT_SUCCESS; } void my_new_handler() { cerr << progname << ": out of store\n"; exit(EXIT_FAILURE); }