global or local

This is a discussion on global or local within the C Programming forums, part of the General Programming Boards category; hey guys...i don't understand why there are global and local variables. why can't we use all the variables as global? ...

  1. #1
    Unregistered
    Guest

    global or local

    hey guys...i don't understand why there are global and local variables. why can't we use all the variables as global? if we declared an integer as global, it takes the same memory as local does. correct me if was wrong.

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    1.) If there were no local variables, you would have to declare all your variables globally, thus a part of your code would look like a "Dictionary" rather than a C program.
    2.) One part of a code could easily modify the contents of a varible any time and it would be difficult for you to trace out what modified it (the changes would ofcourse be reflected globally).
    3.) Variable naming space would shrink. You would not have too many choices on naming variables.
    4.) Memory is allocated to Global variables during compile time itself, shortening the space available to your program.

    It's always a good practice to avoid Global variables as much as possible. But there are places where using it is really an advantage. At the end of the day, you are the coder, the choice is yours.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    you would need things like say, int i, I, i_, I_, i2, I2; for all the loops you use or something. your program will become very unmodular.

  4. #4
    *
    Guest
    You are incorrect.

    Global variables are allocated at compile time. That means that however many bytes it takes to hold all your global variables, is created as a binary block and included as part of the size of the executable. Hardcoded, as it were. Globals are addressed relative to the start of your code.

    Local variables however, are allocated on the fly when a function is called. You may have heard the term 'stackframe'. Whenever a function or procedure is called, a stackframe is created on the stack that contains the function/proc's variables and return value (if any). Then while the variables are in use, they are referenced relative to the start of the function/proc, on the stack. When the function terminates, the calling code grabs the return value (if any), and disposes of the used stackspace holding the stackframe.

    The main reason you have two kinds of variables is for scope. It makes no sense to create a function/proc that uses variables for its own purposes and make those variables available to everyone. To do so increases the link size, RAM requirements, and slows things down.

    Global variables take more processor time to access, because they use a larger pointer and the relative offset is farther. Local variables only need a small offset, so they can be accessed faster from within the function than a global variable.

    If you want to know more about this, read about variable 'scope'.

  5. #5
    *
    Guest
    Ooops, I meant that the original 'Unregistered' poster was incorrect.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you want to become a good programmer, try not to use global variables. Read up about coupling and cohesion of modules/functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. Local vs Global Variables
    By BENCHMARKMAN in forum C++ Programming
    Replies: 5
    Last Post: 07-03-2007, 05:17 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  5. Global Vs Local Variables
    By Waldo2k2 in forum C++ Programming
    Replies: 17
    Last Post: 11-11-2002, 06:51 PM

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