confusion over static variables

P

pauldepstein

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)

{

static int confused_about_static = x;

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
 
P

pauldepstein

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
 
D

Daniel T.

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 );
}
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top