Re: error handlling in recursive function



David Resnick <lndresnick@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...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



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: 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: 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)
  • Re: Any use for recursion?
    ... popped back off the stack, the program counter being changed again. ... Also, if the recursion is deep, e.g. 16 levels deep, then it results ... can be done with a plain loop can be done with a recursive function. ...
    (comp.programming)
  • 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)