Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Program Wide Global Vars in C++?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
22 replies to this topic

#21 larspensjo   Members   -  Reputation: 1557

Like
2Likes
Like

Posted 03 January 2013 - 04:17 AM

I am starting to think that the use of globals is not evil in itself. It is a problem of scale and scope. To take an example, which is unrealistic but possible:

 

Take a C program "PROG" where every heap allocated variable is globally defined. This kind of application is the way programming was done in the old days, before object orientation and ADT was invented (please allow me the simplification). This PROG will be hard to maintain and inflexible when it grows. If the complete source code fits into one screen, it should still be fine. However, as many found out, when PROG grows there is point when maintenance gets unreasonable. It simply takes too much effort to do changes, with severe side effects making it hard to guarantee correctness.

 

If this big PROG is packaged into one single huge class in C++, it will fulfill the requirement of not using globals. But the basic problem still remains, which is a problem of scale. Parameters in a class can be just as problematic as global parameters, if the class is too big (bloated, fat class, ...).

 

From that point of view, stating that global variables are evil is a simplification? The real problem is the size of the scope where a variable (or function and sometimes type!) is available. But I suppose it is much easier to explain "Don't use global variables", than to explain that scopes shall be kept to a minimum size.


Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

Sponsor:

#22 noizex   Members   -  Reputation: 882

Like
2Likes
Like

Posted 03 January 2013 - 06:37 AM

I try to design without globals in mind, but things like debugger/profiler that I have to use and I _need_ access to it from basically every part of my code to dump something on the screen or profile execution just require some kind of global var / singleton (which I don't like and prefer just typical global variable). I tried hard to figure how to pass it around and having it passed through all object chains because I may need it at some point is just plain stupid IMO.

Instead I just create a global instance of Debugger class and in any .cpp file that I need access to it to dump some data or profile part of the code, I use extern. I really can't see how this can be done better - I can't use global functions because I need a state (debug drawing requires geometry to be compiled and drawn each frame etc, so instead of one class I'd have to get some global variables anyway and it will be ugly). Its also only for debugging, so in release mode my code is global free anyway.

I think people who are 100% against globals are some hard to convince OO-freaks.

Edited by noizex, 03 January 2013 - 06:41 AM.


#23 Khatharr   Crossbones+   -  Reputation: 3037

Like
0Likes
Like

Posted 03 January 2013 - 11:38 PM

Take a C program "PROG" where every heap allocated variable is globally defined. This kind of application is the way programming was done in the old days, before object orientation and ADT was invented (please allow me the simplification). This PROG will be hard to maintain and inflexible when it grows. If the complete source code fits into one screen, it should still be fine. However, as many found out, when PROG grows there is point when maintenance gets unreasonable. It simply takes too much effort to do changes, with severe side effects making it hard to guarantee correctness.

Very true. It's important to know why things can be problematic rather than just that they can be problematic. This frees people to make more enlightened decisions about design because they learn the underlying problem (and how to fix it) rather than just the rule.

Edited by Khatharr, 03 January 2013 - 11:40 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS