delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/07/19/09:02:22

From: kers AT hplb DOT hpl DOT hp DOT com ()
Newsgroups: comp.os.msdos.djgpp,comp.lang.c
Subject: Re: Recursions and Static declarations....?Is this wrong...
Date: 19 Jul 2002 11:08:36 GMT
Organization: Hewlett-Packard Laboratories, Bristol, UK
Lines: 41
Message-ID: <ah8rvk$h1v$1@murdoch.hpl.hp.com>
References: <5a91d0ef DOT 0207181004 DOT 49e67056 AT posting DOT google DOT com>
<ah8k0q$lqv$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE>
<ah8p8r$b1l$7 AT murdoch DOT hpl DOT hp DOT com>
<ah8qp0$sdn$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE>
NNTP-Posting-Host: cdollin.hpl.hp.com
Mime-Version: 1.0
X-Trace: murdoch.hpl.hp.com 1027076916 17471 15.144.94.165 (19 Jul 2002 11:08:36 GMT)
X-Complaints-To: usenet AT murdoch DOT hpl DOT hp DOT com
NNTP-Posting-Date: 19 Jul 2002 11:08:36 GMT
X-Newsreader: knews 1.0b.1
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In article <ah8qp0$sdn$1 AT nets3 DOT rz DOT rwth-aachen DOT de>,
	Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de> writes:
> In comp.os.msdos.djgpp kers AT hplb DOT hpl DOT hp DOT com wrote:
>> In article <ah8k0q$lqv$1 AT nets3 DOT rz DOT rwth-aachen DOT de>,
>> 	Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de> writes:
>>> 
>>> No, you can't.  At least not sensibly, in the way you did it.  You
>>> shouldn't be recursing for this, in the first place, nor should you
>>> ever use static variables to keep state across a recursion call.
> 
>> Not even for such things as recording recursion depth for prettyprinting?
> 
> Not in a useful way, if the static is local to the recursive function.

void printIndented( FILE *sink, Tree t )
    {
    static int depth = -1;
    int i;
    depth += 1;
    for (int i = 0; i < depth; i += 1) fputc( ' ', sink );
    if (isLeaf( t ))
	fprintf( sink, "=%s\n", leafString( t ) );
    else
        {
        fprintf( sink, "*%s\n", nodeString( t ) );
        for (int i = 0; i < treeKidCount( t ); i += 1)
            printIndented( sink, treeKid( t, i ) );
        }
    depth -= 1;
    } 

perhaps the example is too simple to raise the point you're addressing?

[It usually turns out in the end, in C anyway, to be easier to pass
the depth in as a parameter. This can make it harder to share interfaces,
which is annoying.]

-- 
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019