PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > error handlling in recursive function
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
error handlling in recursive function

Réponse
 
LinkBack Outils de la discussion
Vieux 30/05/2008, 14h22   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut error handlling in recursive function

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)
  Réponse avec citation
Vieux 30/05/2008, 14h51   #2
David Resnick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: error handlling in recursive function

On May 30, 9:22 am, pereges <Brol...@gmail.com> 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.

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
  Réponse avec citation
Vieux 30/05/2008, 16h05   #3
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: error handlling in recursive function

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


void *operationfails(...) {
int *p;

if (p = malloc(whatever)) {
/* do your thing on it */
}
return p; /* which is NULL for failure */
}

for example.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 30/05/2008, 16h46   #4
Jens Thoms Toerring
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: error handlling in recursive function

David Resnick <lndresnick@gmail.com> wrote:
> On May 30, 9:22 am, pereges <Brol...@gmail.com> 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@toerring.de
\__________________________ http://toerring.de
  Réponse avec citation
Vieux 30/05/2008, 18h17   #5
David Resnick
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: error handlling in recursive function

On May 30, 11:46 am, j...@toerring.de (Jens Thoms Toerring) wrote:
> David Resnick <lndresn...@gmail.com> wrote:
> > On May 30, 9:22 am, pereges <Brol...@gmail.com> 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...

-David
  Réponse avec citation
Vieux 30/05/2008, 20h02   #6
user923005
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: error handlling in recursive function

On May 30, 10:17am, David Resnick <lndresn...@gmail.com> wrote:
> On May 30, 11:46 am, j...@toerring.de (Jens Thoms Toerring) wrote:
>
>
>
>
>
> > David Resnick <lndresn...@gmail.com> wrote:
> > > On May 30, 9:22 am, pereges <Brol...@gmail.com> 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..._handling.html
  Réponse avec citation
Vieux 30/05/2008, 20h27   #7
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: error handlling in recursive function

In article <19b77d08-9400-4bcc-b5ef-112545eda14f@x35g2000hsb.googlegroups.com>,
user923005 <dcorbit@connx.com> wrote:
>On May 30, 10:17=A0am, David Resnick <lndresn...@gmail.com> wrote:


>> Sure, and a fourth is to pass down a pointer to a variable to use for
>> error reporting.


>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...
--
"I want to be remembered as the guy who gave his all whenever
he was on the field." -- Walter Payton
  Réponse avec citation
Vieux 30/05/2008, 20h48   #8
Antoninus Twink
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: error handlling in recursive function

On 30 May 2008 at 19:27, Walter Roberson wrote:
> If the routine so invoked does not terminate with longjump


What's longjump?

(Think I'll make it as a pedant? )

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 12h53.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17623 seconds with 16 queries