### Global Variables

Hi,

I have a program that has t declared as a global variable with `double t; //the time variable `. Well, That is initialized to zero by the method, init(); and then is incremented by a variable tau which is also defined globally. So the program chugs along until it gets to the timestep method (included below). It appears that both t0 and t become NaN after the 0 step in the for-loop. I do not understand this part because I am not varying t. By the time that I reach the end of the code where I add tau to t, t is already NaN and so I get a time that is NaN. As you can imagine, for time evolution, this doesn't work too well.

 ``12345678910111213141516171819202122232425`` ``````void timeStep() { double t0 = t; do { // int x = 31; // int y = 61; cout << "t = " << t << endl; if(debug) cout << t0 << endl; for(int y = 0; y sizeX / T / framesPerSec); glutPostRedisplay(); glFlush(); }``````

Additionally, I am getting some more NaN errors where I wasn't getting them anymore with some of my other variables when they are defined globally.

Is this just an issue with globally defining variables in c++ or possibly something else?

Thomas
Last edited on
I dont understand your problem, but it problably has to do with one of those two things:
- The scope of variables; the variables are bounded to the functions there declared in: global variables (declared outside any function) are accesible from all functions. This means that variable V inside main() is another variable as variable V inside somefunction() and chances on one of them doesnt have any effect on the other. Also, unless you declare a variable as static it's value will be lost when the end of its scope is reached.
- You don't initialize the variables when using them as local variables. This will give problems when using var+=value.
The top of my function is

 ``123456789101112131415161718192021222324252627`` ``````using namespace std; //using namespace itpp; //////////////////////////////////VARIABLE DECLARATION//////////////////////////////////////////// double T = 5; double k = 2; //determines the speed of motion, used in e^ikx bool debug = false; //True for debugging, used mainly in glut but also gives nice console output every step of the way. double t; //the time variable double m = 1; //mass double c0 = 1; //beginning speed double xcenter = 32; //x coordinate for the center of the pressure wave double ycenter = 64; //y coordinate for the center of the pressure wave double sigmaXY = 5; //determines the width of the pressure wave in the x double sigma0 = 6; //determines the sigma for the daf double tau = 0; //time step as determined by the nyquist condition, meant to be less than nyquist double temp; //Temp double for various calculations, resets as needed double speed; //velocity profile double delX = 1; //Grid spacing double tempP[upperM]; //The values of the array for the Pk double bar = 0.1; //This is the difference between xi and xj, yi and yj, etc. Can be tuned for more accuracy. double ** grid; double tMax = 10000; //This is tMax, set to something needlessly big so it runs infinitely double framesPerSec = 60; // animation rate for screen redraws bool running; //controls animation double expDelT; double * dafX; double * dafY;``````

and none of those are within a method. From what I can gather, the scope of the variables is such that they can be used in any method without having to be passed. That's what I wanted to happen. Then, most of those, the ones that aren't already given a value, are initilized in the init() method which is included below.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````void init() { if(debug) cout << "a" << endl; t = 0; //resets time double upperBound = 20; //This is the upper Range for the daf calculations tau = (c0*c0)/(k*k*100); grid = new double*[sizeX]; for (int asdf = 0; asdf < sizeX; ++asdf) grid[asdf] = new double[sizeY]; //Generates your gaussian for(int y = 0; y

After this, I believe that the global tau is defined as (c0*c0)/(k*k*100) and the variable t is set to zero. I did this with the intention that when you press enter in the GLUT window, it resets the whole thing.

The problem is, that in the method timestep(); which I included above, time for some reason turns into NaN. I am not sure why.

I hope that helps for clarification.

I also have a question, if I have a variable defined globally, then would I need to declare it in every method that I use it anyways?
I think all those variables should be declared inside a class/struct.

Globals only need to be declared in every file that uses them:
 ``1234`` ``````//Definition: T a=0; //Declaration (will cause errors if in the same file as the definition): extern T a;``````
What do you mean by using a class?

All the variables that I have declared are being used in the file they're being used in. Is that a problem?

Sorry for the stupid questions, I'm learning C++ on the fly as I'm taking a computational physics class.
Topic archived. No new replies allowed.