confusion over static variables

Discussion in 'C++' started by pauldepstein@att.net, Jan 16, 2006.

  1. Guest

    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
    , Jan 16, 2006
    #1
    1. Advertising

  2. Guest

    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
    , Jan 16, 2006
    #2
    1. Advertising

  3. Daniel T. Guest

    In article <>,
    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.
    Daniel T., Jan 16, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,365
    Tony Morris
    Oct 19, 2004
  2. Anup Daware
    Replies:
    2
    Views:
    329
    Anup Daware
    Feb 2, 2007
  3. Neviton
    Replies:
    9
    Views:
    446
    Alf P. Steinbach
    Sep 17, 2007
  4. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    695
    Army1987
    Oct 8, 2007
  5. DJB
    Replies:
    1
    Views:
    91
Loading...

Share This Page