Question:
C prog q: why is it a rule of thumb to avoid global variables? thanks?
2009-05-27 23:45:13 UTC
C prog q: why is it a rule of thumb to avoid global variables? thanks?
Three answers:
?
2009-05-28 00:09:56 UTC
A global variable can be accessed by any portion of the program. When you are working by yourself, and have complete control and knowledge of every function and routine that is accessing that variable, then it may not be an issue. As your projects become larger and you start working with others, though, global variables become a bad idea because you don't know who is accessing it, what they have modified it to, and how that affects other areas of the program. So it's best to learn how to not use global variables and compartmentalize your program (learn data hiding, basically).
rlamoni
2009-05-28 00:51:27 UTC
There are a number of reasons to avoid global variables. Here are a few.



1. Variables in the global namespace make it hard for two programmers to work on the same project. One programmer might use a global variable for one thing in his/her code and another use it for something else. This makes problems hard to track down because two people wrote pieces of code that work, but the system as a whole does not.



2. Global variables are always consume memory, even when not in use. When variables are declared in functions (on the stack) or through memory allocation (on the heap) the space they use can be freed up when they are no longer needed (by deallocation, garbage collection, or stack “popping”.) A global variable's memory cannot be freed until the program closes.



3. Global variables make multi-threaded programming harder. Many programs, these days, are multi-threaded to take advantage of multi-core processors and other OS parallelization mechanisms. When you have a global variable that is accessed by two or more threads you must perform expensive (and sometimes complicated) locking to prevent the threads from interfering with each other. Of course, by the time you are skilled enough to write multi-threaded applications, you will have stopped needing global variables because your mental toolkit of data communication will include a lot of better design patterns.



One thing you should be aware of is the difference between global “variables” and static (kind of like global) constants. Static constants are perfectly ok in most cases. Since they don’t change, none of the three reasons I listed to avoid global variables apply. A smart complier will put those constants in a special place in memory that will allow the OS to make them faster and protect them from malice. Since these constants do not change you don’t want to pass them around your program because making copies on the stack or in the heap is wasteful.



This is a good question that I wish beginner programmers would ask more often. There are a sad number of intermediate programmers out there with bad habits because their teachers never gave them good reasons to avoid picking up these habits. The teacher just said “don’t do it, you will regret it” and the beginner thinks “but if I can save typing and planning overhead why should I follow some arbitrary rule.”
John S
2009-05-28 00:09:46 UTC
First of all, that is not true. Global variables can be very very important.



Would you really want the same data stored in more than one variable? Or would you really want to have a massive number of arguments passed to a function?



The rule should be not to abuse global variables.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...