C++ Learning Community Forum
September 11, 2010, 12:41:40 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Global Variables  (Read 688 times)
zaqufant
Farmer Brown
Dr. of C++ology
****
Posts: 963


Harder, better, faster, stronger.


View Profile
« on: January 14, 2009, 02:56:57 PM »

I know everyone preaches against the evils of global variables. But, I don't think I've ever been sure why they're bad. Is it to avoid name clashes in programs? Is that the only reason global variables are bad? Or why are they so bad?
Logged
biggoron
C++ Freak
***
Posts: 351


View Profile
« Reply #1 on: January 14, 2009, 03:57:45 PM »

It goes against encapsulation, which is the idea that all data should be as invisible as possible to everything. That is, data should only be visible where it is necessary to be visible, and no-where else. Then again, I'm not sure why encapsulation is so important, just that it seems to be important Smiley

I guess it depends on whether or not you fear 'raptors.
Logged

charlie
Is the meadow on fire?
Dr. of C++ology
****
Posts: 730


CsGYh8AacgY


View Profile
« Reply #2 on: January 14, 2009, 05:15:36 PM »

Encapsulation is important in part because it helps you maintain control over how and when data gets changed.

If you have a global variable, then any code can change that value at any time. It is much harder to keep track of when the data changes. It is also much harder to add functionality. If you want a change to some data to trigger an event, encapsulating that data will make implementing your code easier. If you just have a global variable, then you'd have to find and change all code that modifies the variable and somehow ensure that any new code that needs to modify the variable also triggers the event.

There are other reasons, too, I'm sure.
Logged
oulyt
C++ Freak
***
Posts: 340



View Profile
« Reply #3 on: January 14, 2009, 10:02:57 PM »

geez. i better stop using global variables then >.<
Logged
zaqufant
Farmer Brown
Dr. of C++ology
****
Posts: 963


Harder, better, faster, stronger.


View Profile
« Reply #4 on: January 14, 2009, 11:06:20 PM »

Say you make a struct that has all the variables you need for that program. So, you make one instance of that struct to organize all the variables you need for that program, is that a good solution for global variables. This way you only have to pass one variable to each function that need those variables, and the class can still be local to main. So like:

Code:
stuct Program
{
  int size;
  int numof;
  char* name;
  vector<BITMAP*> images;
}Program;

...
int main()
{
  Program program;
  ...
  while(not finished)
     UpdateProgram(program);
  ....
}
So is it ok to organize all the individual pieces of data you need in a struct that you will only use once? Is this a good solution?
Logged
biggoron
C++ Freak
***
Posts: 351


View Profile
« Reply #5 on: January 14, 2009, 11:45:33 PM »

I don't think changing how you pass it around really changes the level of encapsulation:

Code:
struct AllVariables
{
    int a, b, c;
};

void doSomething(AllVariables& x)
{
    x.a = x.b = x.c = 1;
}

int main()
{
     AllVariables x;

     doSomething(x);
     return 0;
}

Code:
int a, b, c;

void doSomething(int& x, int& y, int& z)
{
    x = y = z = 1;
}

int main()
{
     doSomething(a, b, c);
     return 0;
}

Small difference, it's all still visible, just with your way it has to be explicitly passed.
Logged

ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1241



View Profile
« Reply #6 on: January 15, 2009, 07:39:31 AM »

the x within the main function can't be "seen" by the doSomething function unless it explicitly passed to it. That's generally a good thing.  Though it is because of how you've scoped things there Tongue hehe sorry just had to poke.  Roll Eyes

I think changing how you pass things might change it a little bit, but it's more about scope than anything really.

I think encapsulation is good because I think it enhances changeability. If data is tightly encapsulated, you should be able to remove the implementation (leaving the interface) and create a new implementation without issues like having to deal with a ton of now undefined variables. You'd only need to worry about a few function calls, to keep everything in the rest of the program working smoothly.

Also, being the security nerd i am (security is like one of my fun side computer hobbies haha) I say that it helps from that standpoint as well because if you have say a buffer overflow issue, encapsulating your data should minimize the area that an exploit could effect. I don't know for sure on that one, but it sure makes good sense. If I'm wrong please correct me though.
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
charlie
Is the meadow on fire?
Dr. of C++ology
****
Posts: 730


CsGYh8AacgY


View Profile
« Reply #7 on: January 16, 2009, 08:52:06 PM »

The struct does have benefits, but if you use an instance of a struct as a global variable then you have the same issues you have if all the members were global.

Note that you have two instances of Program in that code. One is global and is named Program and the other is local to main and is named program.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!