Bloomberg LP Interview Question
Developer Program Engineersvoid out_of_store() {
cerr << "\noperator new failed: out of store\n";
return;
}
class A {
X *x;
Y *y;
Z *z;
A(){
std::set_new_handler(out_of_store);
x = new X;
y = new Y;
z = new Z;
}
};
class A {
- avadh.786 September 29, 2010X *x;
Y *y;
Z *z;
A(){
x = new X;
y = new Y;
z = new (nothrow)Z;
}
'nothrow' is a special object defined in header <new>. If the memory allocation fails for Z, instead of throwing a bad_alloc exception (which must be handled), 'new' returns back a NULL pointer and continues the execution of the program.