Global C++ variables

Posted 16 August 2010 - 02:41 PM

Hi. I read somewhere that global variables can be defined by putting them outside of any function and that in doing this you would cause them to be available across your whole program, even if it is split between files.

I tried this however received the error "Variable myVar is not within scope at line blah blah in file blah blah.....".

So my question is this: Does file1.h need to include main.cpp in order to access its globally defined variable? If not then what is the problem? To me, including my main.cpp file which contains my main(), just for 1 variable, would seem like super bad programming practice.

Re: Global C++ variables

Posted 16 August 2010 - 03:34 PM

The extern keyword, of course, I remember reading about that somewhere but it apparently escaped my memory. Thanks for the help!

...and yes I know all about function arguments however the code I supplied was massively simplified and taken out of context, so while passing arguments into functions is super awesome, it is much more efficient to use a global variable in my case.

Re: Global C++ variables

Posted 17 August 2010 - 04:31 AM

As many others have said, you should refrain from using Globally Scoped variables. In smaller programs, you might be able to get away with a global const. When writing anything larger, you are asking for trouble. Especially with a non-descriptive name such as 'var'. You also lose all encapsulation of that variable within it's scope, anything can touch it. Though you may think that is not a problem in your particular instance, it is bad programming practice. If you want that variable to reflect the change in main.cpp, pass by reference. If you only want a copy made for that function, pass by value. These methds are simple and will make your code better to use.

Now at least you can trace it, control changes, and alleviate some of the potential pain of using a global.

You can use the same trick for quasi singletons and shared structs. C++ can avoid globals with class patterns. For C, this is really the only option for a global state. Well, aside from doing it properly and passing parameters.

Re: Global C++ variables

Posted 17 August 2010 - 11:21 AM

Wow you guys reeeaaaally are against global variables. I honestly didn't realise they were so frowned up.

In all honestly I am quite new to c++ (couple of months) and my last language was javascript so it's been a big jump but I guess its knowing things like this that separate the noobs (me) from the pros (you)

I cant link you any of the code but essentially I am using the GTK gui library and I have a user interface being initialised in my main, and in separate files I have other aspects of the gui such as dialogs, message boxes etc. Now in some of these files containing the code to produce dialogs etc, I need to access certain gui objects that were created in my main.

So if I have 20 objects or variables or arrays etc that I need to access from a dialog, I would surely need to pass like 20 arguments during its creation in order to have pointers to all of these things that my dialog hypothetically needs to access.

And since most of the time many of these things will not change (but it remains important that they can be changed) it just seems more efficient to store their pointers and values in global memory.

Re: Global C++ variables

Posted 17 August 2010 - 12:02 PM

The big issue with globals is that any part of your program can change them, at any time. Figuring out who did what when is near impossible in big bowl of spaghetti code. And any code that liberally uses globals is pretty much guaranteed to be a steaming pile of debug nightmare.

Passing pointers to data costs an int and makes explicitly clear what part of the program needs what data. If there are logical groupings of data, you can pass a struct. Indeed, if you have a set of data points that are always being referenced together, it probably should be a struct.

Variables should exist for the shortest time possible. They should have the narrowest scope possible. Functions should be given only the information they need to do their job. If you can keep to this, your code can be lean, mean, and easy to clean. Globals are, by their nature, the opposite of this.

Re: Global C++ variables

Posted 18 August 2010 - 12:55 PM

Some interesting points there, thanks for the post. I am actually continuing work on a program designed in this way by a previous employee and it actually is a nightmare to debug, I expect for the reasons you mentioned.

I think I am convinced though and will try to edit out as much of the dirty global variables as I possibly can