Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TheGlobalVariableThatIsn't

Status
Not open for further replies.

XerxesTheMighty

Programmer
Jun 15, 2003
72
US
I have global variable, after every time I use it, I clear it (like it should). But at about line 2510 or so the global variable doesn't exist. The compiler still recoginizes it as global and therefor doesn't give an error, but at runtime the global variable doesn't exist. So I just create a temporary variable for the time being. What causes this? Is it something I'm doing? Or is it the compiler can only handle so many global variables (I have at least 35 global variables, and a whole lot of functions.)

--------------
"Ask a question and your a fool for three minutes, don't ask it and your a fool for a lifetime." -Chinese Proverb

Xerxes Dynatos
 
35 isn't very many, and supposedly the compiler can take an indefinite amount of variables. It is hard to guess what your problem is with this information, but you might want to make sure the debug error is really what it appears to be. Try setting your compiler to warning level 4. (assuming you are still using VC++)

-Bones
 
Some possibilities:

* You have accidentally defined a local variable/function with the same name as the global variable.
* You have declared a class with a member variable/method having the same name.
* Your program has corrupted memory by the time it reaches your line #2510, and has either confused the debugger, or has caused some other problem that is preventing you from seeing your variables.

By the way, global variables are unique in that they have permanent addresses in your program's data segment. That means they are not allocated dynamically and never go away.

So I suggest that when your program starts, you go into the debugger and see what address your global variable has. Then later on line #2510, use the debugger to peek at that exact same address in memory.

Even if the debugger no longer thinks your global variable is there, at least you can inspect memory there and see the value that your variable has at that time. It might give you some hints about what is going on to inspect that area of memory as your program is running.

Also, if you think you might have a local or class variable that accidentally has the same name as your global variable, you can check by looking at the address of your variable (type "&myvar" instead of "myvar" in your debugger's watch list). If the address suddenly changes, then bingo, you're now looking at a _different_ "myvar" that is probably a local or class variable.

The global "myvar" is still there in the background, just masked out because it has the same name as a local.
 
I believe you have somewhere a local variable with the same name as the global one. Try to use :: to access only global one.

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top