Dynamic memory allocation in CPP, new operator returns NULL value if memory allocation is unsuccessful

Q.  During dynamic memory allocation in CPP, new operator returns _________ value if memory allocation is unsuccessful.

- Published on 17 Jul 15

a. False
b. NULL
c. Zero
d. None of these

ANSWER: NULL
 

    Discussion

  • Brijesh   -Posted on 23 Oct 15
    new operator returns null value if memory allocation is unsuccessful. The new operator will throw a std::bad_alloc exception on failure and this can be handled by exception handling as follows.
    try
    {
    char* cPtr = new char[50];
    }
    catch (std::bad_alloc&)
    {
    // Handle error
    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)