I usually never see test for new in C++ and I was wondering why.
Foo *f = new Foo;
// f is assumed as allocated, why usually, nobody test the return of new?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As per the current standard, new never returns NULL, it throws a std::bad_alloc instead. If you don’t want new to throw(as per the old standard) but rather return NULL you should call it by postfixing it with ‘(std::nothrow)‘. i.e.
Of course, if you have a very old or possibly broken toolchain it might not follow the standard.