Re: When to check the return value of malloc
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Mon, 28 Jan 2008 05:05:14 +0530
Kelsey Bjarnason wrote:
<snip>
And that's exactly what xmalloc does: it allows me to request the 64MB
I'd like, but prevents me from detecting the failure and adjusting the
request.
You complain that malloc causes problems. Even assuming you had a
point, your solution is worse than the problem. At least with malloc,
*I* can adjust to conditions. *I* can choose to use a smaller buffer.
*I* can detect the error and decide that perhaps I should try another
task, then check back later to see if memory is available. With your
code, no such options are available.
Granted, with your "user function" or whatever you call it, I can, in
principle, say "try again, with a smaller amount of memory", but that
doesn't help, as I cannot tell the calling code that the change has
occurred; it asked for 64MB, what was *actually* allocated was 16MB,
how the hell does it know that? It doesn't - there's no way to
respond back that the request was not honoured.
You can set a global variable from the call-back to tell the higher
level code the amount of memory that was actually allocated. Ugly, but
doable.
<snip>
.
- Follow-Ups:
- Re: When to check the return value of malloc
- From: Kelsey Bjarnason
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- References:
- When to check the return value of malloc
- From: Marty James
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- From: Keith Thompson
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- From: Keith Thompson
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- From: Keith Thompson
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- From: Kelsey Bjarnason
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- From: Kelsey Bjarnason
- Re: When to check the return value of malloc
- From: Malcolm McLean
- Re: When to check the return value of malloc
- From: Kelsey Bjarnason
- When to check the return value of malloc
- Prev by Date: Re: When to check the return value of malloc
- Next by Date: Re: Linux Kernel Source
- Previous by thread: Re: When to check the return value of malloc
- Next by thread: Re: When to check the return value of malloc
- Index(es):
Relevant Pages
|