recovery from NULL==malloc(size)
Posted Mar 14, 2008 21:38 UTC (Fri) by
jreiser (subscriber, #11027)
In reply to:
CERT C Secure Coding Standard: last call for reviewers by ajross
Parent article:
CERT C Secure Coding Standard: last call for reviewers
OOM situations on modern (multiprocess, virtual memory) operating systems are not recoverable errors at the level of a single application.
Sometimes they can be, but it requires planning ahead, perhaps from the very beginning of the application which wishes to recover. One strategy is to malloc a safety buffer (say, 5MB), scribble on it, not use it after that, then free() it upon running out. In some situations this buys enough for orderly shutdown, switching modes, or postponement of the application.
... more elaborate rules, like "Don't use dynamic memory allocation at all."
By itself, such a rule is as simple as possible; but implementing it might be cumbersome and elaborate. Or, it might be a true inspiration: make a pre-pass which computes a close upper bound, perform all the malloc() at one time [and do anything else needed to "fully" consume the memory resources], then run the main application which sub-allocates space from the reservations.
(
Log in to post comments)