Possible Duplicate:
Will new return NULL in any case?
Say i have a class Car and i create an object
Car *newcar = new Car();
if(newcar==NULL) //is it valid to check for NULL if new runs out of memory
{
}
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.
On a standards-conforming C++ implementation, no. The ordinary form of
newwill never returnNULL; if allocation fails, astd::bad_allocexception will be thrown (thenew (nothrow)form does not throw exceptions, and will returnNULLif allocation fails).On some older C++ compilers (especially those that were released before the language was standardized) or in situations where exceptions are explicitly disabled (for example, perhaps some compilers for embedded systems),
newmay returnNULLon failure. Compilers that do this do not conform to the C++ standard.