If malloc returns void * then what does the new operater returns when it encounters a failure ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

If malloc returns void * then what does the new operater returns when it encounters a failure ?

Also please tell me if you could describe how the variable will react with the operations and how can I detect that a failure has occurred ?

17th Mar 2019, 3:14 PM
Sahil Bhakat
Sahil Bhakat - avatar
3 Answers
+ 5
If something goes wrong, it will return a null pointer: int *p; if((p = (int*)malloc(25*sizeof(int)) == NULL) { // something went wrong }
17th Mar 2019, 3:26 PM
Anna
Anna - avatar
+ 3
the new operator indicates failure by throwing an exception of type "std::bad_alloc" unless “nothrow” is used, in which case it returns a nullptr : https://code.sololearn.com/c3aw8gUs5i5w/#cpp
17th Mar 2019, 4:16 PM
MO ELomari
0
Thanks for your help
20th Mar 2019, 3:22 AM
Sahil Bhakat
Sahil Bhakat - avatar