GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Aug-2005, 07:53
Peter11111 Peter11111 is offline
New Member
 
Join Date: Jul 2005
Posts: 16
Peter11111 is on a distinguished road

The Use of GLobal variables


I 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  
Old 12-Aug-2005, 08:39
Kacyndra's Avatar
Kacyndra Kacyndra is offline
Member
 
Join Date: May 2005
Location: Maryland
Posts: 226
Kacyndra will become famous soon enough
Quote:
Originally Posted by Peter11111
I 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?


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  
Old 12-Aug-2005, 08:43
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough
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.
  • Many times a variable does not need to persist for the entire life of the program, or between function calls, so why have it using up that space? Often local (to the function using it) static variables suffice.
  • Similarly, a variable need not be visible to all compilation units.
  • Perhaps more importantly, imagine that you are using someone else's code. You are calling their function, which internally makes use of some global variable. This means that you cannot be sure from the function signature alone what you need to do to call the function properly. Do you have to set some global first? After the function is called, does some global variable contain useful information now? It is a source of confusion, an accident waiting to happen really, if globals are involved in interfaces that may be used by others.
In C++, there is never a need for true globals. A programmer can always use namespaces to restrict the scope of her variables, functions, classes, etc.

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  
Old 12-Aug-2005, 09:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by QED
In my experience (like Kacyndra's), yes. The C/C++ community generally frown upon the use of globals.


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 GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 05:43.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.