"Richard Bos" <> wrote in message
> "Malcolm McLean" <> wrote:
>
>> In very critical applications, you will want to recover gracefully from a
>> memory allocation failure. This adds very substantially to the costs of
>> writing the code, and is also difficult to test. However if the program
>> must
>> under no circumstances terminate, this is what you must do. xmalloc()
>> supposes that termination can be used as a last / first resort, depending
>> on
>> the program.
>
> Tell me, have you ever pasted a large graphic into a report?
>
You shouldn't use the xmalloc() handler as part of the "normal" flow control
of the program.
Let's say we are allocating an image of 1 million bytes. At the same time we
allocate a hundred byte string to hold the image name. Let's also say that
the user experiences one allocation failure per day of using the program, on
average.
A simple calculation will show that, assuming 50 working weeks of 5 days
each, he can expect the failure to be in the hundred byte allocation about
once every 40 years. For the vast majority of "lose a day's work" type
critical applications, that sort of failure rate is acceptable. it is much
lower than the chance of a hardware failure in the same time. On the other
hand one failure a day isn't acceptable, so we've got to handle it rather
better. But the failure-handling routines tend to add considerable
complexity to code, and it's hard to test.
The point of xmalloc() is that even if the allocation does fail, we can
choose to give the user a chance to get some more memory. However we don't
have special error-handling recovery code. It's simply not worth coding and
testing - and they tend to be hard to test - custom error handlers for such
rare problems, unless the future of the business is at stake if we have to
crash. It's a misallocation of scare programming resources.
Note that even if the once in forty years problem does come up, we can still
give the user the chance to close down some applications. If he can't get a
hundred bytes from the system, anywhere, it is likely that he'll have to
reboot anyway.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm