Re: xmalloc
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Sat, 23 Jun 2007 20:06:40 +0100
"Richard Heathfield" <rjh@xxxxxxxxxxxxxxx> wrote in message news:kbednX2M0Nuh9-DbnZ2dnUVZ8qOtnZ2d@xxxxxxxxx
Malcolm McLean said:In which case would call regular malloc()
Continuing the error-handling thread.
I am floating
void *xmalloc(int sz)
{
void *answer;
assert(sz >= 0);
if(sz == 0)
sz = 1;
answer = malloc(sz);
if(!answer)
{
fprintf(stderr, "Can't allocate %d byte%c\n", sz, sz == 1 ? ' ',
's'); exit(EXIT_FAILURE);
}
return answer;
}
as a solution to the malloc() problem.
What malloc() problem? And this "solution" suffers from the same problem
as any other "solution" of the same kind - it's no earthly use in a
library, and libraries are where malloc calls belong.
There are two reasons why it's no earthly use in a library.
For one thing, it displays a message on stderr, which isn't much cop in
a system where stderr messages are ignored - e.g. a Win32 GUI app. But
that's trivial to fix - either remove the message completely, or add a
callback or a pointer to an error code, so that the application can
worry about how to display the message.
The second problem is also quite easy to fix, and it's this - the
function calls exit(), thus taking the decision to quit the program out
of the hands of the programmer. Therefore, the programmer won't call
this function himself, and won't be able to call any other function in
the library that /does/ call it.
Here's my suggested replacement, which incorporates all the fixes I see
as being necessary:
#include <stdlib.h>
void *xmalloc(size_t sz)
{
return malloc(sz);
}
You call it for trivial allocations on the basis that if the computer
won't give a few bytes of memory, not much can be done.
Whether that is true depends on the problem you're trying to solve. 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.
However you are doubling the cost of the software with that strategy, all because a 2GB machine might refuse to give a few hundred bytes of memory. If it is running a life-support machine, fair enough, but most software doesn't.
On the other hand you could argue that xmalloc() is a bad idea, because no library that uses it could ever find its way into a life-support machine.
On the other hand, you could argue that the life support machine is more likely to blow a fuse than to exit in xmalloc(), so you need a backup anyway.
I take your point about vandalised stderrs.
Maybe we should put an assert fail in there as well / instead.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
.
- Follow-Ups:
- Re: xmalloc
- From: Nick Keighley
- Re: xmalloc
- From: Eric Sosman
- Re: xmalloc
- From: Richard Heathfield
- Re: xmalloc
- References:
- xmalloc
- From: Malcolm McLean
- Re: xmalloc
- From: Richard Heathfield
- xmalloc
- Prev by Date: Re: I don't see where I'm clobbering the memory
- Next by Date: Re: I need some cleanings tips and advice.
- Previous by thread: Re: xmalloc
- Next by thread: Re: xmalloc
- Index(es):
Relevant Pages
|