![]() |
|
#1
|
||||
|
||||
Global variable goes out of scope.I know how to use global variables across different files - with the extern keyword etc.
My problem is with a global variable going out of scope in its own file. I declared three global variables at the top of the file, outside of any functions. I had a vector of strings, v1. An array of ints, num[], and an int variable, number. The vector v1, the array num[] and the variable number, are three global variables within the one file, test_the_code.cpp. The vector v1 can't be assigned values with push_back() within the function test_code(). When I do that, then during debug, if I try to get the debugger to give the value of the vector elements, I get told "variable is out of scope". A few minutes/moments later, the IDE crashes. It's as if the debugger is trying to read an infinte amount of data - as if there were no end to the number of elements in the vector. The array num[] can't be assigned values within the function test_code(). Just before the assignment, num[0] = 1, during debug, the array has "random" values for its elements. (It doesn't even keep its globally assigned element values.) After the assignments, the values are unchanged - still "random". The assignment n1 = num[0] doesn't seem to work either. Although uninitialised, n1 had a value of zero. After the assignment, its value was 1. It wasn't even the random value the debugger gave for the value of num[0]. Only the global variable, number, works. Inside the function test_code(), it keeps its globally assigned value of 100. When I assign a value of 99 to it, it takes that value. Conclusion: global vectors and arrays go out of scope within a function in the same file, but ordinary variables don't. Any ideas, suggestions, hints ?? I can get global variables to work ok in an ordinary console app, but not in a windows app. Why not ?? Here is my code - from the windows app. Using Visual C++ 2005. I created a windows app with a single form. I put a buttton on that form. When I run/debug the app, and I click on the button, it takes me to a button-click() function that calls my test_code() function. I have edited the code a bit a to save space, since a lot of it is just initialisation of windows components. The only relevant code in Form1.h is the button_click() function at the end. Its name is Testerbtn_Click(). All other code not included is just a mass of files created by the IDE when I created the windows app. None of that code has been altered Quote:
CPP / C++ / C Code:
Last edited by LuciWiz : 18-Apr-2007 at 14:05.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
||||
|
#2
|
||||
|
||||
Re: Global variable goes out of scope.It's a debugger/IDE problem.
|
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Variable scope question | wmmccoy0910 | MySQL / PHP Forum | 5 | 23-Sep-2006 16:25 |
| Global Variable declaration in dialog based MFC Application | Sureshpr | MS Visual C++ / MFC Forum | 1 | 31-May-2006 23:51 |
| global variable | coder | MS Visual C++ / MFC Forum | 2 | 26-Feb-2006 14:50 |
| [Tutorial] Pointers in C (Part II) | Stack Overflow | C Programming Language | 0 | 27-Apr-2005 18:36 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The