Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of global variables in simulation code [duplicate]

Possible Duplicate:
Are global variables bad?

I am writing a simulation code making use of material and energy specific data. This data is stored in global arrays, because once uploaded, they are used during the simulation and should be accessible by most of the functions.

I read everywhere that it is not good practice to use global variables. Could someone explain me or point me to material on the web explaining how I could avoid using global arrays in simulation application coding while massive data arrays need to be used. I try to code in C++ and make use as much as possible of object oriented features.

Thanks in advance for your help.

like image 398
noste99 Avatar asked Jul 23 '11 10:07

noste99


1 Answers

You are right about the fact that, using globals are not recommended. You can declare those unrelated golbals inside a namespace,

//Globals.h
namespace Globals
{
  extern int a[100];
  extern double d;
}

and define them in a .cpp file.

//Globals.cpp
int Globals::a[100] = { ... };
double Globals::d = 3.14;

Now use them as Globals::a, Globals::d etc. My answer is in code management perspective.

like image 166
iammilind Avatar answered Sep 23 '22 13:09

iammilind