Why are global variables considered bad?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Adam01
    New Member
    • Aug 2008
    • 83

    Why are global variables considered bad?

    I was thinking of adding some global vars to my program when I stoped to think why they are considered harmful.
    I read on a google result that they can get mixed up with local variables, but that can be simply fixed with a prepended global_ or something on the lines of that.

    I remember reading somewhere about them causing security or memory leaks or something like that.

    Is that why? or is there more to it?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Global variables are not so harmful by themselves as well as they are very 'volatile' in a sense that everything can change those variables at will. If a global variable doesn't contain the value you'd expected you have to scrutinize all your code to find the quilty part.

    Global variables also don't go well with more than one thread of execution.

    kind regards,

    Jos

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Read this first: http://bytes.com/topic/c/insights/73...obal-variables

      Usually a global variable is a replacement for thought.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by JosAH
        Global variables also don't go well with more than one thread of execution.
        Actually, this criticism applies to any variable with static duration. Such variables can be either global or local.

        In C, global variables always have static duration; while local variables only have static duration if they are defined with the static keyword. C++ may have additional ways to specify a variable with static duration.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          In C, global variables always have static duration;
          You may be confusing scope with linkage.

          That is, in C global variables have global scope. That is, they can be accessed by any function in the implementation file where the global variable is defined.

          To do that, they must exist for the life of the program.

          Now if the global is defined as static, it means the linkage is internal. Variables with internal linkage are accessible only in the current implementation file. By default, global variables have external linkage which means they can be accessed from other implementation files if those files use the extern storage class specifier.

          For local variables in functions, coding them static means that they are acessible only in the current implementation file (where the function is defined) plus the static allows the variable to exist after the function completes. So, it has internal linkage and function scope. Static global variables also have internal linkage but file scope.

          Comment

          Working...