Question:
c language - global variables?
good enough
2009-07-14 12:02:19 UTC
If i got it right, global variables are very helpful - you don't have to define in every function the variable, but you can make it global. the question is, is there any problems using them - i was told it's preferable not to use them for they can cause memory problems, but i can't understand why???
Six answers:
anonymous
2009-07-14 12:25:12 UTC
In a small program you might not see a problem with them but if you see a need to use them you should really re-think your design.



http://www.c2.com/cgi/wiki?GlobalVariablesAreBad
R.C.
2009-07-14 19:29:36 UTC
Whether or not they cause memory problems is not the reason the use of global variables is a bad practice.



It is bad practice because using a global variable breaks modularity.



In a large program, it is very handy to have "black-box" reusability of your functions, without worrying about what's in a global variable, or what it does to a global variable. If a function relies on the presence of certain globals, then its behavior may not be consistent.



That said, there are times when it is either necessary or extremely convenient to use global variables. This, however, should be a very occasional exception, not the norm.
ctBuckweed
2009-07-15 16:28:17 UTC
Hi Greenie,



Yes, global variables don't have to be defined within every function. However, other functions that are called may use and modify the same global variables whereas the calling function may not even be aware that that the variable has been changed.



This may lead to erratic program behavior and even program corruption.



Global variables should not be used for passing parameters to some called function. Instead call the function with arguments to specify the functions behavior.



Some global variables are quite useful. Take for example:



int Debug;



main()

{

 Debug = 1;



 doSomething(); /* which does many calls to subroutines */

}



aWayDownSubroutine()

{

 if (Debug == 1)

 {

  printf("I am way down in a subroutine\n");

 }

}
?
2009-07-14 20:11:12 UTC
Global variables are almost as nasty as gotos. They go against all that is good in structured programming and modularity. Having said that, you do occasionally need to use them (just as you occasionally might be justified in using a goto) but they should always be a last resort and be used very sparingly indeed.
jplatt39
2009-07-14 19:31:00 UTC
Number one, when something goes out of scope, it is not in memory at all. Since a global variable is never out of scope, it is always in memory. Of course the corollary to that is if it is a dynamic array, you have to make absolutely perfectly sure to do garbage collection and otherwise cleean it up. Number two, despite it being always in scope it is very very easy to forget and give a temporarrry variable the same name -- therebye both hiding it and taking up even more memory.



Global variables are a pain in the you-know-where. Don't bother.
Packman
2009-07-14 19:12:15 UTC
global variables can be tricky to keep track of if you use them in many different functions (figuring out the value stored in them). And yes it does take up more memory than just passing a variable or a pointer to each function that is needed.


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