error handlling in recursive function

P

pereges

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)
 
D

David Resnick

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.

n.b. this is a case where exception throwing is nice, as with minimal
fuss it gets you back to the level that wants to handle the error
cleaning up all in between. But as we're in C, not an option

-David
 
J

Jens Thoms Toerring

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
 
D

David Resnick

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...

-David
 
W

Walter Roberson

Yet another one is to use signal()/raise().

If the routine so invoked does not terminate with longjump
(and longjump was already proffered earlier in the list) then
when the routine returns, execution will resume with the return
of raise() (which will have a value of 0 if successful, non-zero
otherwise.)

signal()/raise() does have the advantage that the invoked routine
is able to access library functions, and is able to access static storage
that is not volatile sig_atomic_t (undefined behaviour if the
invocation of the signal'd routine does not come from raise()).
Effectively, signal()/raise() becomes a method for storing a hidden
global pointer to a subroutine that gets called when raise() is used...
nothing you couldn't easily duplicate. Hmmm, I bet there has already
been an IOCC entry (or five) that relied upon this...
 
W

Walter Roberson

What's longjump?

'jmp' was considered to be a machine-specific instruction, so in
TC9 longjmp will be deprecated and the more general longjump substituted
instead. ;-)
(Think I'll make it as a pedant? :) )

Always hoped that I'd be a pedant
Knew that I would make it if I tried (If I tried)
Then when we retire we can write the corrigenda
So they'll still talk about us when we've died

(With apologies to Mr. Webber)
 
C

Chad

If the routine so invoked does not terminate with longjump
(and longjump was already proffered earlier in the list) then
when the routine returns, execution will resume with the return
of raise() (which will have a value of 0 if successful, non-zero
otherwise.)

signal()/raise() does have the advantage that the invoked routine
is able to access library functions,

I don't get what you mean when you say "signal()/raise() does have the
advantage that the invoked routine is able to access library
functions, "

Can you give an example of this?

Chad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top