If you use set_new_handler and your handler function is called, is errno guaranteed to be set, the way it is on a return of 0 from malloc? Or is it better to use strerror(ENOMEM)? errno works on Microsoft C++ and GCC, but that still leaves the question of whether it’s guaranteed.
If you use set_new_handler and your handler function is called, is errno guaranteed to
Share
I don’t think
errnois good enough for detecting dynamic memory allocation failures. Looking at N3337, specifically 3.7.4.1 Allocation functions:and footnote 35 (this is only a indicative and non-normative):
Now, heading on to the C standard draft, N1570 and a look at 7.5 Errors
<errno.h>:It appears that
errnomay be set by amallocfailure but this is not required.Also 7.22.3 Memory management functions from the N1570 does not specify that
mallocor friends are required to seterrno.My suggestion would be to stick to what the standard guarantees and use the exception (
std::bad_alloc) thrown bynew(i.e. not use the no-thrownew).