Like Tree67Likes

Global Varibles

This is a discussion on Global Varibles within the C++ Programming forums, part of the General Programming Boards category; No. I admit it; you were right all along. ........ parameters! ........ local statics! Globals for the WIN! Soma...

  1. #61
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,177
    No. I admit it; you were right all along.

    ........ parameters!
    ........ local statics!

    Globals for the WIN!

    Soma

  2. #62
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Hurray. Then gimme somes Like's already.
    After this beating, I need a booster.

  3. #63
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by motocross1 View Post
    Are you happy now?
    How about ending the thread, jerk?
    Never. I continue to pray for you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #64
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    If I had a nickel every time I've heard that one, wow, let me tell you, I'd have 5-cents.
    (sorry I called you a jerk)
    MK27 likes this.

  5. #65
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    Quote Originally Posted by motocross1 View Post
    I currently do my globals by creating them in Global.h ( extern int myInt; ) and then initializing them near the top in various cpp files ( int myInt=123; ).
    But, what is the "professional" way?
    (other than not use globals, which I find hard to believe is possible)
    Thanks
    I have read much, but not all the items in this thread. So if this duplicates something already said, I am sorry. I also realize that many are trying to draw this thread to a close. I saw that someone suggested using a variable name tag to make it clear which variables are globals. This is also a good idea.

    I am not fluent in C++ but I am in C. So if you are using C++ in:
    a) a fully object oriented way with full use of classes, templates, containers and the standard library or
    b) using it mostly as C with Classes,
    then my advice may or may not be applicable.

    If you are going to use globals (yes, there has already been enough discussion about that part of the topic) and each of the global variables is going to be used in almost all of the same places, then using global.h (or something similar) is fine.

    However, if only a limited few are going to be used in a small number of places, I would suggest that you NOT put all the declarations in a single file. I would suggest that you split them into several files. I would also consider making that header file with the global variable declarations separate from the header file with the function and class declarations. Here you may want to use <name>_g.h or g_<name>.h for the file name. The likely <name> is the name of the .cpp file where you are putting the initialization. Then include the these individual files only where they are needed to read or modify the global variables. Then it minimizes the places that specific globals can be accessed (and accidentally be changed) and this will minimize the coupling mentioned earlier in the thread.
    motocross1 likes this.

  6. #66
    Registered User
    Join Date
    Oct 2006
    Posts
    2,300
    Quote Originally Posted by motocross1 View Post
    Hurray. Then gimme somes Like's already.
    After this beating, I need a booster.
    do you really expect people here to coddle you? what have you done in your limited tenure on this forum to merit such treatment? if you want to feel good about yourself, go volunteer at a soup kitchen. if you want intelligent discourse on the merits (or lack thereof) of using global variables, including comments contrary to your own opinions, then this is the place for it. if you require emotional validation for your technical expertise, you're going to live a cold, lonely life.

  7. #67
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,620
    Quote Originally Posted by motocross1 View Post
    There are 3 groups of people who come to this forum:
    1) Students
    2) Professionals who help the Students
    3) Me - Someone needing information, not advice
    Then what am I? I'm student. I help students, but I'm no professional. I form a 4th group! The group of "Hobbiests who help the Students and other hobbiests"
    Elkvis, MK27, stahta01 and 1 others like this.
    Devoted my life to programming...

  8. #68
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    pheininger,
    Thanks for the plug, but trust me, the Professionals seem to only listen to those with a high number of posts.

    Elkvis,
    "if you want intelligent discourse on the merits (or lack thereof) of using global variables"...
    YES, let's start that up again today.

    I will WIN this time.
    I have the right weapon now.
    It came to me last night before going to sleep.
    My subconscious is, hmmm... "freakin awesome" - HA.

    Are you all ready to lose this debate now?

    One word...
    namespace

    Feel free to explain why namespace exits if a class can "encapsulate" everything.
    You will fail. Period.

    Accept defeat, HA!!

  9. #69
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    Looks global to me:
    Namespaces - C++ Documentation

  10. #70
    Registered User
    Join Date
    Feb 2010
    Posts
    93
    The forum's own website:
    FAQ > C++ Namespaces - Cprogramming.com

    WOW, I beat you guys.
    I can't believe it.

  11. #71
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by motocross1 View Post
    The forum's own website:
    FAQ > C++ Namespaces - Cprogramming.com

    WOW, I beat you guys.
    I can't believe it.
    Not exactly. There are no global variables in that article. Namespaces are primary applied to types, not instances. Yes, you could declare a variable in a namespace, making it effectively a global, but that's the same bad practice.

    Put another way: yes, your classes are defined globally. Of course. Just like types such as "int" are defined globally. That's not the same thing as a particular int instance. Eg, C++ strings are defined in the standard namespace (hence, accessible globally as std::string, a type). But AFAIK, there are no global variables defined in the standard namespace.

    Basically you are conflating objects with classes, but I am sure you know the difference. Going back to something from your post #22:

    Code:
    struct myStructData{ string name; int num; bool isReady; };
    extern vector<myStructData> StructData;
    Defining struct myStructData globally* is fine and dandy. Probably necessary.** It's declaring vector StructData globally that is the issue.

    * or in a namespace, which is better. The type is still globally accessible, like std::string, but organized into a collection of related things. This way, if you declare a type such as myStructData in namespace myNamespace, you can declare a totally different myStructData in some other namespaces and the names will not collide. That's the point of namespaces. Otherwise, in a large project using a lot of third party libraries, everyone would have to be paranoid about the names they used for fear of redefining something with the same name somewhere else.

    ** you can nest a class definition inside another class definition, making it an inner private class which is not available globally. This is a good practice if the class has no application beyond the class it is nested inside. You can do the same thing with structs, typedefs, etc.

    Make sense?
    Last edited by MK27; 03-06-2012 at 09:05 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #72
    Registered User
    Join Date
    Oct 2006
    Posts
    2,300
    Quote Originally Posted by motocross1 View Post
    Accept defeat, HA!!
    you are behaving as if this is some sort of competition. you are wrong.

    you asked for advice/opinions. people provided them. what more do you want?

  13. #73
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by motocross1
    Feel free to explain why namespace exits if a class can "encapsulate" everything.
    Feel free to explain what is encapsulation and what are namespaces, and the reason will become obvious

    Quote Originally Posted by MK27
    There are no global variables in that article.
    There are two "global variables" in that article, but they were declared in an unnamed namespace. Consequently, they suffer from the same problems as global variables of the "declared extern" variety, but with the mitigating factor that the reader only has to be concerned about the functions in the translation unit changing them.

    Quote Originally Posted by MK27
    Namespaces are primary applied to types, not instances.
    I would rather say that namespaces are just a way to group names so as to avoid name collision.

    Quote Originally Posted by MK27
    But AFAIK, there are no global variables defined in the standard namespace.
    std::cout and std::cin are effectively global variables in the std namespace.
    MK27 and Elkvis like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #74
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Motocross1:

    Aside from the very critical and important points that some of the other guys have made about encapsulation and coupling, there is another downside to using globals. When declaring a global variable you're adding that variables name to the global namespace, which essentially means you can't use that name for anything else if you want your code to be readable or understandable at all.

    Consider the following code:
    Code:
    #include <iostream>
    
    int myNumber = 8;
    
    int doMath(int myNumber = 5)
    {
    	myNumber += myNumber;
    	return myNumber;
    }
    
    int main()
    {
    	int myNumber = 3;
    	myNumber = doMath(myNumber);
    	myNumber += doMath();
    	std::cout << myNumber;
    	return 0;
    }
    One would have to step through the code line by line, or actually run it, to be certain of what the output would be. Now imagine that the variable isn't called "myNumber", imagine that it's called "index" or "key" or "pi" or some other commonly used variable name. You're essentially removing that name from the pool of available variable names for all the code that is written after or before you declared that global variable, unless you don't mind the source looking like my above example. This means you will have to pick less fitting names or resort to some obscure pseudo-hungarian naming scheme (such as "g_index"), which in turn means your code will become less readable.

    This isn't the only problem. Imagine if you were ever to write some code that was to be reused by someone other than you, if you put global variables all over it, it is more than likely that there will be some sort of naming conflict when someone else has named their function the same as one of your global variables. Now i know you've said that the code you are writing is only for yourself, but you might aswell start learning proper techniques now. If you stick with global variables now, you will have trouble down the road when someone demands that your code lives up to some form of expectations. (Such as being even slightly readable)

    Now all this i wrote is only partly why global variables are frowned upon, as others have stated that there are other more critical problems with global variables.

    Also on a sidenote, you seem to think that "the professionals" on this board are trying to force you to structure your code according to some strange illogical agenda. Just know, that global variables are frowned upon not only on this board, but in the entire professional computer-science industry, globally.

    Even the Wikipedia article on the matter speaks poorly on global variables. Take a step back and think about this, the entire world disagrees with you, maybe you should try and re-read this thread with an open mind, and actually make an effort to understand what some of the others have touched upon.

    Or....you could continue to argue that you are right and the world is wrong.
    rogster001 likes this.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  15. #75
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Neo1 View Post
    maybe you should try and re-read this thread with an open mind, and actually make an effort to understand what some of the others have touched upon.
    No tish. There is a ton of great information and explication in this thread. Motocross1, you are wasting a big opportunity to understand some fundamental concepts by not asking specific questions about the parts that are unclear! There is nothing wrong with saying, "I don't get this", or "could you explain this further", etc, rather than skipping over them and presuming (or pretending) that they must be irrelevant.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 5 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string varibles??
    By Labmouse in forum C++ Programming
    Replies: 2
    Last Post: 08-23-2007, 08:16 AM
  2. Declaring Varibles
    By lbrault in forum C++ Programming
    Replies: 6
    Last Post: 02-05-2003, 11:23 AM
  3. extern varibles
    By manwhoonlyeats in forum C Programming
    Replies: 5
    Last Post: 12-17-2002, 11:43 PM
  4. C Functions and varibles
    By mart_man00 in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 01:11 PM
  5. varibles
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 06-24-2002, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21