Re: xmalloc
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Sat, 23 Jun 2007 20:08:53 +0000
Roland Pibinger said:
<snip>
You can often see programs that check the return value of malloc but
you hardly (practically never) see any code that actually handles the
OOM (out-of-memory) condition (e.g. frees some pre-allocated memory).
Strange, isn't it?
Yes. It's a great shame.
Moreover, C libraries usually crash when the caller provides
insufficient resources or invalid arguments. In that sense abort() is
preferable to exit() for OOM.
Well, I do see your point, but personally I'd prefer for the program to
make sensible choices that minimise inconvenience to the user.
For
example, it may well be possible to switch to a static solution which
is perhaps not as fast as the dynamic solution but which will
nevertheless do the Right Thing in a reasonable time.
The 'static solution' may cause a 'stack' overflow.
So make sure it doesn't, by avoiding stress to your 'stack'.
BTW, how would you
handle the second type of OOM, 'stack' overflow? The same way as OOM
for dynamic memory?
One of the reasons dynamic memory is so useful is that there's a heck of
a lot of it to go around, compared to a typical 'stack' - but the other
great advantage thereof is that you /can/ detect an OOM condition with
malloc, whereas you can't with 'stack' overflow - at least, not
portably. Nevertheless, you can still do quite a lot with a 'stack'
that doesn't involve a significant risk of overflow (the adjective
being necessary only because the C Standard gives us very little in the
way of guarantees about 'stack' capacity - in practice, nowadays it is
only a problem if you treat the 'stack' rather recklessly).
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.
- References:
- xmalloc
- From: Malcolm McLean
- Re: xmalloc
- From: Richard Heathfield
- Re: xmalloc
- From: Roland Pibinger
- xmalloc
- Prev by Date: Re: I don't see where I'm clobbering the memory
- Next by Date: Re: xmalloc
- Previous by thread: Re: xmalloc
- Next by thread: Re: xmalloc
- Index(es):
Relevant Pages
|