What shall we do?
The life of thousand people depends on that function you need to call, passing to it some fresh allocated memory. The operator new (unless the placement one is called) deals with OOM condition throwing a bad_alloc or returning a null-pointer in case the nothrow version of it is used.
But as programmer what can you do when a bad_alloc is thrown or a null-pointer is returned?
There are several options, but the most "nifty" one is the following.
When the operator new is not able to allocate the required memory it calls a function, at this point the function can try to free some memory, throwing an exception or exit the program. Exiting the program is not a good option I have to say, indeed the caller of the operator new (or operator new [] for the matter) expects a bad_alloc (or a derivation of it) or a nullptr (in case the nothrow was used).
A programmer is able to specify the function to be call in case of OOM with the following function:
the operator new will keep calling the specified function every time it tries to allocate memory and it doesn't succeeded. A programmer can exploit this mechanism in the following way:
- Allocate a the programming startup a bunch of memory reserving it for future uses.
- Install the new handler that will free the reserved memory, in case the reserved memory was already release then throw bad_alloc.
The following code does exactly what described:
issuing a ulimit -v 100000 before to run it (in order to decrease the memory that can be used), the output is the following:
FREEING SOME MEMORYSUCCEEDEDNO MORE MEMORY TO FREEterminate called after throwing an instance of 'std::bad_alloc'what(): std::bad_allocAborted (core dumped)
1 comment:
You explained this complex topic in a very simple way. Thank you for sharing.
C++ tutorial in urdu
Post a Comment