473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

confusion over static variables

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 '06 #1
2 1977
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 '06 #2
In article <11**********************@z14g2000cwz.googlegroups .com>,
pa**********@att.net 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.
Jan 16 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ashkon | last post by:
Can someone please help explain the following behaviour, or point me to a post that explains it? While i was debugging one of my company's programs i noticed some objects behaving strangely. I...
5
by: dharmesh Gupta | last post by:
i have a multifile program namely bpl.cpp-contains main() function idr.h ( class definitions) idr.cpp ( the implementation of the functions in the classes described in idr.h) bpl1.h ( contains...
4
by: Fao, Sean | last post by:
Hello All, I'm reading The C++ Programming Language and I'm having a misunderstanding of part of the code in the book. Specifically, I'm not understanding the line: Date...
3
by: Ken Brannigan | last post by:
Hello, I am a little confused regarding the creation of a C# DLL that will be called from ASP.NET. From what I have read ASP.NET is Multithreaded. My undersatnding of this is that more than one...
12
by: Rene | last post by:
I understand that if I don't assign a *local* variable before I use it, the compiler will generate a "Use of unassigned local variable" error. What I don't get is why doesn't the compiler just...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
4
by: Jesika | last post by:
I'm coming from a 3 year C background with some basic knowlegde on the Win32 API. I just started learning C# a day ago and I have a couple questions that I need cleared up that just have me very...
2
by: Anup Daware | last post by:
Hi Group, I have a little confusion over the use of static class in C#. I have a static method in my static class. This method reads an xml and returns a collection of objects. This collection...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.