Re: error handlling in recursive function



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 function
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)
Options seem to be:
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
.



Relevant Pages

  • Debug my program please.
    ... I have an error in to recursive function aggiungiat first recursion. ... !character,allocatable:: Parola ... Reinizializzazione della parola if (.not. ...
    (comp.lang.fortran)
  • Re: error handlling in recursive function
    ... 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 ... checking for it at each invocation. ... Use setjmp (before entering recursion) and longjmp to hop back on ...
    (comp.lang.c)
  • Re: Timing Rescordset
    ... The recursive function I am using first check to see if it has something, ... avoid levels of recursion that lack substantial advantage. ... XML authors use the same node name in a different heirarchy? ... To reconstruct, you'll have to query the db for each level, in much the same ...
    (microsoft.public.data.ado)
  • Re: error handlling in recursive function
    ... calling function(the one which called the recursive function in the ... checking for it at each invocation. ... recursion. ... Ok, global variables are EVIL, ...
    (comp.lang.c)
  • Re: Traversing shallow CTreeCtrl
    ... Why not write a recursive function ... that takes exactly on HTREEITEM ... number of levels usually ends up being more difficult than a simple recursion. ... LvlThreeNode = MyTree.GetNextItem; ...
    (microsoft.public.vc.mfc)