Thread: Global Variables?

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    15

    Global Variables?

    As a general rule, why is it a good idea to avoid the excessive use of global variables. Please answer in detail, better if in points.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I can say from experience, debugging a lot of global variables is tricky.

    In a project I was involved with, there were two global variables with similar names (two sub-states from the same struct). Because the state needs to be visible to all parts of the code (so one section didn't go into a state that shouldn't run when the other is in some state), use of local variables was not a preferred option.

    A state from one variable was accidentally given to the other. Because the modification was made in another place of the program and because the names looked similar, it was very hard to find.
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I think this sums it up pretty well: Global Variables Are Bad.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Please answer in detail, better if in points.
    So you can copy/paste directly into your homework answer?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    32
    Quote Originally Posted by Hasnat Abul View Post
    As a general rule, why is it a good idea to avoid the excessive use of global variables. Please answer in detail, better if in points.
    You can find it in K&R

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In case to avoid the probability of what salem said i will say only this:
    Imagine a large program which uses global variables.It is extremely possible that manu variables are going to conflict with others, in case of same names...So you have to resolve which one is shadowing the others etc.. This does not only make it difficult from you, but extremely unreadable for the others.

    For more,see what the above post says

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global & global variables
    By aqeel in forum C Programming
    Replies: 1
    Last Post: 09-25-2009, 12:32 PM
  2. Global Variables
    By samuelmoneill in forum C Programming
    Replies: 4
    Last Post: 04-15-2009, 06:27 AM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Global Variables
    By Sn0mAN in forum C Programming
    Replies: 3
    Last Post: 08-08-2007, 12:54 PM
  5. C global variables
    By Dohojar in forum C Programming
    Replies: 18
    Last Post: 02-25-2002, 12:00 PM