![]() |
|
#1
|
|||
|
|||
The Use of GLobal variablesI am writting code for my thesis, and have about 1000 lines of C code. I have used quite a few global variables, and my issue is, should I keep them or get rid of them.
To put it simply. Is the use of global variables frowned upon by the general C community, such as yourselves? |
|
#2
|
||||
|
||||
|
Quote:
i think that having lots of global variables is bad practice. you should make them local (well depending on it's function ofcourse)... at least whats what my professors keep telling me __________________
Xrum! |
|
#3
|
||||
|
||||
|
In my experience (like Kacyndra's), yes. The C/C++ community generally frown upon the use of globals. And yet, you will see plenty of programmers using them here and there, sometimes out of laziness or bad habit.
Of course, global constants are a different thing. In C, I have never encountered any resistance to using global constants, even using them liberally. Of course, the question of the required scope is still valid. If you need some constant only within the scope of a single function, then use a local static. Others with more experience can no doubt shed more light on the subject. Matthew |
|
#4
|
|||
|
|||
|
Quote:
Very well said. I would only like to add that one reason that many people frown on gratuitous use of globals is that it can give rise to certain types of bugs for which debugging is very difficult. A simple typo in some function way, way down in the hierarchy can cause it to change a global variable instead of changing the local variable that it was supposed to be working on. In the course of debugging, you find that a certain variable is not what is is supposed to be. You look and look and look at the function that you know is supposed to set that variable, and can't figure out why it has the right value in that function but has the wrong value when it is being used somewhere else. Of course you don't look at other functions, since they weren't supposed to affect that particular variable. Such things might not cause any apparent problems during testing, but can cause Bad Things to happen in unpredictable and unrepeatable ways when you or other users run the program with different input sequences. I wouldn't go so far as to say that globals are "never" necessary, but I would say that I strongly dis-recommend using globals unless you feel that they are really, really, really important to your application. If I were your boss (or instructor, or spiritual advisor, or ...) you would have to show me why you felt it was beneficial to use each and every global variable in your program. Just my opinion, of course. YMMV (Your Mileage May Vary.) I have had a little experience at University level research, but not lately. Some research advisors (in departments other than Computer Science, of course) write programs only for their own use. They run and run and rerun the programs until they get the answer they are looking for and then never run them again. Sometimes charitably known as "heuristic" program design and "ad hoc" debugging. Or is it "ad hoc" program design, and... Oh, well. Lots of times these were the guys who were proud of the fact that, "I taught myself BASIC on my Apple II in 1982," and even though they picked up C (well, sort of) somewhere along the way, they still use globals for everything, to keep from having to type all of all those annoying function arguments all over the place. (They should have taught themselves touch typing, and how to use a text editor more productive than notepad, so they wouldn't be scared of typing. Oh, yeah, they also learned that their BASIC programs would run faster and take less memory if they stripped out all of the comments.) Regards, Dave Last edited by davekw7x : 12-Aug-2005 at 09:57.
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| passing variables out of an iframe by url | JUNK KED | MySQL / PHP Forum | 5 | 31-Jul-2007 10:33 |
| variables return to previous value after i try to set them | nasaiya | MS Visual C++ / MFC Forum | 2 | 14-Jun-2005 01:43 |
| Function and Array (w/ reference variables) question | brookeville | C++ Forum | 15 | 07-Dec-2004 02:11 |
| getting variables from $_GET & $_POST | Dunc | MySQL / PHP Forum | 2 | 29-Mar-2004 06:45 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The