Re: A solution for the allocation failures problem
- From: vippstar@xxxxxxxxx
- Date: Tue, 29 Jan 2008 04:26:56 -0800 (PST)
On Jan 29, 1:47 pm, jacob navia <ja...@xxxxxxxxxx> wrote:
1:It is, with proper design it becomes easier too.
It is not possible to check EVERY malloc result within complex software.
3:That was not a solution.
A solution like the one proposed by Mr McLean (aborting) is not
possible for software quality reasons. The program must decide
if it is possible to just abort() or not.
Solution:That does not guarantee anything, it depends on the OS, and the
1) At program start, allocate a big buffer that is not used
elsewhere in the program. This big buffer will be freed when
a memory exhaustion situation arises, to give enough memory
to the error reporting routines to close files, or otherwise
do housekeeping chores.
implementation of free/malloc.
Also it makes your program slower, and what if the big buffer
allocation fails?
Would you exit because your program failed to allocate resources it
would not use?
What if no allocation fails? The buffer is only a waste of resources.
2) xmalloc()free it anyway, free(NULL); does nothing.
static int (*mallocfailedHandler)(int);
void *xmalloc(size_t nbytes)
{
restart:
void *r = malloc(nbytes);
if (r)
return r;
// Memory exhaustion situation.
// Release some memory to the malloc/free system.
if (BigUnusedBuffer)
free(BigUnusedBuffer);
BigUnusedBuffer = NULL;Undefined behavior, you pass size_t where a variadic function expects
if (mallocfailedHandler == NULL) {
// The handler has not been set. This means
// this application does not care about this
// situation. We exit.
fprintf(stderr,
"Allocation failure of %u bytes\n",
nbytes);
unsigned int.
fprintf(stderr,"Program exit\n");What about previous allocations that have been done with xmalloc?
exit(EXIT_FAILURE);
}
memory leak.
// The malloc handler has been set. Call it.Memory leak because you don't give the user a chance to free previous
if (mallocfailedHandler(nbytes)) {
goto restart;
}
// The handler failed to solve the problem.
// Exit without any messages.
exit(EXIT_FAILURE);
allocations
The recovery handler is supposed to free memory, and reallocate theSo if the allocation for BigUnusedBuffer succeeds but the allocation
BigUnusedBuffer, that has been set to NULL;
after the callback fails, we will enter a loop of free-ing/allocating
the big buffer, great.
mr Jacob, I suggest you read a book or two on program structure &
designing.
.
- Follow-Ups:
- Re: A solution for the allocation failures problem
- From: vippstar
- Re: A solution for the allocation failures problem
- References:
- A solution for the allocation failures problem
- From: jacob navia
- A solution for the allocation failures problem
- Prev by Date: Re: A solution for the allocation failures problem
- Next by Date: Re: A solution for the allocation failures problem
- Previous by thread: Re: A solution for the allocation failures problem
- Next by thread: Re: A solution for the allocation failures problem
- Index(es):
Relevant Pages
|
|