This is pseudocode for the operator new:
while (true)
{
Attempt to allocate size bytes
if (the allocation was successful)
return (a pointer to the memory);
// The allocation was unsuccessful; find out what the
// current new-handling function is
new_handler globalHandler = set_new_handler(0);
set_new_handler(globalHandler);
if (globalHandler)
(*globalHandler)();
else
throw std::bad_alloc();
}
Questions :
1) Why first time 0 is passed as parametr to set_new_handler function?
2) It says that when allocation failed, new_handler function invoked, try to allocate momory and if and only if cant makes more memory, it return pointer to the start of allocated memory or if it cant, it throws bad_alloc exeption or returns null pointer and else body works, which throws a bad_alloc exeption. My question is why new_handler function sometimes throws exeption, if it can return null pointer and else body would do this ?