Velocity Reviews > C++ > confusion over static variables

# confusion over static variables

pauldepstein@att.net
Guest
Posts: n/a

 01-16-2006
I have tried to get help over debugging issues and everyone who's
replied has been as helpful as possible. Unfortunately, I haven't
found ways to simplify the issues enough to provide compilable examples
of the problems.

After more experimentation, I think that the key problem is that the
static designation doesn't seem to do what I expected.

Suppose I have a node class which contains a get_equity function which
is recursive

double node::get_equity(int x, int y)

{

if (y == 0)
// code to to define and return get_equity (x, 0) is here.

if ( y > = 1)

{ confused_about_static = y + 2;
return g ( get_equity (x, y - 1) , confused_about_static);

// g is a function of two integer variables defined elsewhere
}

}

This is a hugely simplified version of my code. Assume that we start
with positive input and that the recursion is finite.

Suppose we apply get_equity to (x, y) = (0, 2).

This is what I would hope and expect (but I think this is my error).

confused_about_static is first set to 0.

Since y >= 1, confused_about_static is now reset to y + 2 = 4.
The value g (get_equity (0, 1) , 4) is then returned.

To return this, we need to find get_equity(0, 1). My intention now is
that confused_about_static is now set to y + 2 = 3

Hence the final value to be returned would be g (get_equity (0, 0), 3)

This is probably my confusion. However, this was my reasoning. As I
see it, lines like

static int x = 5; means set x to 5 when you first come across this
line. However, if the line static int x = 5; is part of a loop, you
_do_ not_ keep resetting x to 5, each time you meet that line of code.
The line static int x = 5; only has an effect on the first pass.

What seemed to be happening in my code was that the static variable was
retained from earlier applications of the function. So I apply
get_equity to (2, 3). This sets the static variable.

Later I apply get_equity to (3,4). Now (and this is what I don't want)
my problem is that the static variable is set to the value it had when
I applied get_equity to (2,3).

What I want is the following: each time I apply get_equity to a
coordinate pair, I want the computer to think it is doing that
computation for the first time.

However, when I recursively define get_equity (x...) in terms of
get_equity(x-1...), I only what it to implement the line static int
confused_about_static = ... once when the function is applied to x.
I don't want another initialization when the function is applied to x -
1.

Any suggestions?

Sorry if this is garbled. It's really difficult to be clear about
something you don't understand.

Thank you,

Paul Epstein

pauldepstein@att.net
Guest
Posts: n/a

 01-16-2006
Sorry, there is at least one error in my post above, but it shouldn't
be an obstacle to my main point. The function is an integer function
int node::get_equity(.....

Thank you,

Paul Epstein

Daniel T.
Guest
Posts: n/a

 01-16-2006
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I have tried to get help over debugging issues and everyone who's
> replied has been as helpful as possible. Unfortunately, I haven't
> found ways to simplify the issues enough to provide compilable examples
> of the problems.

At least make an attempt to provide compilable code...

> After more experimentation, I think that the key problem is that the
> static designation doesn't seem to do what I expected.
>
>
> Suppose I have a node class which contains a get_equity function which
> is recursive

If I were you, I would work real hard to make this a simple loop rather
than a recursive function. The whole problem would probably become much
clearer.

> What seemed to be happening in my code was that the static variable was
> retained from earlier applications of the function. So I apply
> get_equity to (2, 3). This sets the static variable.
>
> Later I apply get_equity to (3,4). Now (and this is what I don't want)
> my problem is that the static variable is set to the value it had when
> I applied get_equity to (2,3).
>
> What I want is the following: each time I apply get_equity to a
> coordinate pair, I want the computer to think it is doing that
> computation for the first time.
>
> However, when I recursively define get_equity (x...) in terms of
> get_equity(x-1...), I only what it to implement the line static int
> confused_about_static = ... once when the function is applied to x.
> I don't want another initialization when the function is applied to x -
> 1.
>
> Any suggestions?

Use a flag.

int recursive( int value, bool recursing = false ) {
if ( recursing ) cout << "recursing" << endl;
if ( value == 1 ) return 1;
else
return value * recursive( value - 1, true );
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Theo Markettos UK VOIP 2 02-14-2008 03:27 PM Neviton C++ 9 09-17-2007 10:21 PM Anup Daware ASP .Net 2 02-02-2007 12:12 PM DJB Perl Misc 1 12-03-2005 11:46 PM SaravanaKumar Java 6 10-19-2004 08:20 AM