Re: error handlling in recursive function
- From: user923005 <dcorbit@xxxxxxxxx>
- Date: Fri, 30 May 2008 12:02:06 -0700 (PDT)
On May 30, 10:17 am, David Resnick <lndresn...@xxxxxxxxx> wrote:
On May 30, 11:46 am, j...@xxxxxxxxxxx (Jens Thoms Toerring) wrote:
David Resnick <lndresn...@xxxxxxxxx> wrote:
On May 30, 9:22 am, pereges <Brol...@xxxxxxxxx> wrote:
How to to go about this ? Suppose a malloc inside a recursive functionOptions seem to be:
has failed and you want to set the error flag and return it to the
calling function(the one which called the recursive function in the
first place)
1) Propagate the flag back through the stack of recursive functions,
checking for it at each invocation. This is the best way if you need
to, say, release resources in each invocation of the recursive
function, which seems possible given that you are mallocing in the
recursion.
2) Use setjmp (before entering recursion) and longjmp to hop back on
error.
A third option might be to have a global variable (at file scope)
that gets set if an error occurs. Ok, global variables are EVIL,
but this may be one of the cases where their use can simplify
things a bit...
Sure, and a fourth is to pass down a pointer to a variable to use for
error reporting. I gave the answer I did because I interpreted his
question as being also how to reasonably unwind the stack of recursive
invocations when hitting an error condition...
Yet another one is to use signal()/raise(). E.g.:
http://publications.gbdirect.co.uk/c_book/chapter9/signal_handling.html
.
- Follow-Ups:
- Re: error handlling in recursive function
- From: Walter Roberson
- Re: error handlling in recursive function
- References:
- error handlling in recursive function
- From: pereges
- Re: error handlling in recursive function
- From: David Resnick
- Re: error handlling in recursive function
- From: Jens Thoms Toerring
- Re: error handlling in recursive function
- From: David Resnick
- error handlling in recursive function
- Prev by Date: Re: malloc()/realloc() - have I got this right?
- Next by Date: Re: error handlling in recursive function
- Previous by thread: Re: error handlling in recursive function
- Next by thread: Re: error handlling in recursive function
- Index(es):
Relevant Pages
|