+ 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
4 Antworten
+ 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
MO ELomari - avatar
0
Thanks for your help
20th Mar 2019, 3:22 AM
Sahil Bhakat
Sahil Bhakat - avatar