Easy global variable management in C++

Started by
23 comments, last by iMalc 13 years, 7 months ago
Quote:Original post by rip-off
C++ and C use the same rules for global variables.

Not really. C contains some frankly bizarre rules regarding globals. One of which that the OP seems to be relying on is the concept of tentative definitions. In C the following is legal at global scope:
int i;int i;

This creates one definition for i. This is further complicated by the fact that some compilers, for example some versions of gcc, place globals with only tentative definitions into the common segment of object files, which causes all but one to be discarded at link time. The closest C++ analog to this behavior is a static function variable in an inline function. AFAICT C's linkage rules for globals are supposed to be the same as C++'s but I'm not as familiar with the C standard as I am the C++ standard.
Advertisement
I really don't see what is the point of this technique. Declare the variable in one header, make sure it says extern, and in the cpp file actually define it (i.e. without extern).
Quote:
DATA bool active;
DATA string title;
DATA Game* game;


Other than global state being evil in a variety of ways, naming globals with short, cute names a bad idea. Not only is "active" an incredibly vague variable name, it becomes increasingly so when you put it into global state without any context at all as to how it is used. With title, WHAT is the title referring to? I understand "game" is probably your game state, but it isn't exactly clear what that is.

A good rule to follow is that the larger the scope of a variable the more descriptive its name should be. It's probably even worth it to have a prefix for global variables simply because they should be rare and you really do want to underscore when you are manipulating global state.

Don't be afraid to write things like: g_GameTimerIsActive, g_OperatingSystemWindowTitle, g_GameStateManager

Remember, it doesn't cost you anything to write out a few extra words to describe the variable. You don't pay per character or anything. Be descriptive.

With that said, NONE of these things are a good candidate for a global variable.


[Edited by - M2tM on August 30, 2010 7:44:31 PM]
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk
Quote:Original post by SiCrane
Quote:Original post by rip-off
C++ and C use the same rules for global variables.

Not really. C contains some frankly bizarre rules regarding globals. One of which that the OP seems to be relying on is the concept of tentative definitions. In C the following is legal at global scope:
int i;int i;

This creates one definition for i. This is further complicated by the fact that some compilers, for example some versions of gcc, place globals with only tentative definitions into the common segment of object files, which causes all but one to be discarded at link time. The closest C++ analog to this behavior is a static function variable in an inline function. AFAICT C's linkage rules for globals are supposed to be the same as C++'s but I'm not as familiar with the C standard as I am the C++ standard.

Very interesting. Though not entirely surprising, C is a bit of a minefield of weird legacy stuff. Maybe it would have been better to say that "idiomatic" C uses the same rules, but C is often the home of extremely idiosyncratic styles.
I learnt of this exact trick 12-13 years ago. I think I used it in one Uni assignment but that's it.
It's not a real-world useful thing as globals are mostly very rare, and it doesn't save typing, it just confuses others.

It also encourages you to put variables in header files that might best only be global within that cpp file, i.e. preferably in an unnamed namespace or static. Thus now they are exposed to a lot more of your program which makes tracking down where they're used a bit harder.

Overall, it solves the wrong problem.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement