GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Apr-2007, 16:30
Fermat's Avatar
Fermat Fermat is offline
New Member
 
Join Date: Mar 2006
Location: North East of England
Posts: 23
Fermat is on a distinguished road

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:
//Form1.h

#pragma once

void test_code(void);

namespace TesterProject {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();

}

protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ Testerbtn;
protected:

private:

#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
// code to initialise windows components
}
#pragma endregion
private: System::Void Testerbtn_Click(System::Object^ sender, System::EventArgs^ e)
{
test_code();
}
};
}


CPP / C++ / C Code:
//test_the code.cpp

#include <iostream>
#include <string>
#include <vector>

using namespace std;

vector<string> v1;
int num[4]={9,8,7,6};
int number = 100;

void test_code()
{
	v1.push_back("1");
	v1.push_back("2");

	num[0]=1;
	num[1]=2;
	int n1 = num[0];

	number = 99;
}
// end test_code

Last edited by LuciWiz : 18-Apr-2007 at 14:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 16-Apr-2007, 20:41
Fermat's Avatar
Fermat Fermat is offline
New Member
 
Join Date: Mar 2006
Location: North East of England
Posts: 23
Fermat is on a distinguished road

Re: Global variable goes out of scope.


It's a debugger/IDE problem.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 16:31.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.