global variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tony Johansson

    global variables

    Hello!

    I know it's bad design to use global variables. I just want to ask a
    question about them.

    Is global variables and global static variables the same. These are define
    outside any function at the top of the a file where you have them.

    //Tony


  • shumaker@cs.fsu.edu

    #2
    Re: global variables

    Topic titled "Difference between a global int and a global static int"
    seems to be up your alley:


    Topic titled "global static variable help" has some useful info:


    Topic titled "Static needed on global variables" is anothe good one I
    think:


    You can find lots of info by hitting the "Groups" link at google.com
    and searching there, which is how I found the above posts and many more.

    Comment

    • Donovan Rebbechi

      #3
      Re: global variables

      On 2005-05-20, Tony Johansson <johansson.ande rsson@telia.com > wrote:[color=blue]
      > Hello!
      >
      > I know it's bad design to use global variables. I just want to ask a
      > question about them.
      >
      > Is global variables and global static variables the same. These are define
      > outside any function at the top of the a file where you have them.[/color]

      If it's declared as static, it has "internal linkage". This means it is not
      visible outside the translation unit in which it is defined. In C++, there is
      not so much need for file globals, but in C they can be useful. For example,
      if your C program consists of several modules, each which need to store some
      state information, then each module can be implemented as a .c file, and then
      the state information can be stored in internal-linkage global variables.
      Basically, it's one of many ways to do encapsulation in C.

      In C++, I'd be less inclined to create global variables of any kind. I'd be more
      likely to implement a module system by usually putting state information in
      classes and using inheritance to allow different modules to have a common
      interface but differing state information.

      Cheers,
      --
      Donovan Rebbechi

      Comment

      • Donovan Rebbechi

        #4
        Re: global variables

        On 2005-05-20, Donovan Rebbechi <abuse@aol.co m> wrote:[color=blue]
        > On 2005-05-20, Tony Johansson <johansson.ande rsson@telia.com > wrote:[color=green]
        >> Hello!
        >>
        >> I know it's bad design to use global variables. I just want to ask a
        >> question about them.
        >>
        >> Is global variables and global static variables the same. These are define
        >> outside any function at the top of the a file where you have them.[/color][/color]

        [snip]

        Just an add-on -- one can also have internal linkage for functions, which is
        very useful. In C++, you usually do this via unnamed namespaces.

        Cheers,
        --
        Donovan Rebbechi

        Comment

        • Phlip

          #5
          Re: global variables

          Tony Johansson wrote:
          [color=blue]
          > I know it's bad design to use global variables.[/color]

          It's bad luck to make primitive things globally mutatible.
          [color=blue]
          > I just want to ask a
          > question about them.
          >
          > Is global variables and global static variables the same. These are define
          > outside any function at the top of the a file where you have them.[/color]

          'static' data variables at file scope are an excellent encapsulation
          technique. They are not the same because if two translation units both see

          static int foo;

          then each gets its own copy of foo. Changes to one foo won't affect the
          other file, and changes are the biggest problem with global variables.

          --
          Phlip



          Comment

          • Victor Bazarov

            #6
            Re: global variables

            Donovan Rebbechi wrote:
            [color=blue]
            > On 2005-05-20, Donovan Rebbechi <abuse@aol.co m> wrote:
            >[color=green]
            >>On 2005-05-20, Tony Johansson <johansson.ande rsson@telia.com > wrote:
            >>[color=darkred]
            >>>Hello!
            >>>
            >>>I know it's bad design to use global variables. I just want to ask a
            >>>question about them.
            >>>
            >>>Is global variables and global static variables the same. These are define
            >>>outside any function at the top of the a file where you have them.[/color][/color]
            >
            >
            > [snip]
            >
            > Just an add-on -- one can also have internal linkage for functions, which is
            > very useful. In C++, you usually do this via unnamed namespaces.[/color]

            Wrong. In C++ any function in an unnamed namespace still has external
            linkage by default. You give internal linkage to an object or a function
            if you declare it 'static' or 'const' without 'extern' (for objects).

            V

            Comment

            • Alf P. Steinbach

              #7
              Re: global variables

              * Tony Johansson:[color=blue]
              >
              > I know it's bad design to use global variables. I just want to ask a
              > question about them.
              >
              > Is global variables and global static variables the same. These are define
              > outside any function at the top of the a file where you have them.[/color]

              This is not primarily a language question but a terminology question.

              With respect to a some piece of C++ source code S a variable is "global"
              if it's available in all of S plus possibly in code outside S, and it's
              "local" if it's only available within S, possibly not within all of S.

              I.e.,

              "local" => inside only
              "global" => all of inside + possibly also outside

              Used without qualification the meaning is somewhat language dependent; in
              C++ unqualified "local" usually refers to S = a function, and unqualified
              "global" usually refers to S = a translation unit, then meaning a variable
              that's not only available throughout S but also outside S, in all units.

              With that default in place, a "global variable" (unqualified) means a
              variable available throughout the whole statically linked program, S =
              compilation unit, whereas a "global static variable" (qualified) means
              a variable available throughout a single compilation unit, S = the code
              from the variable declaration to the end of the compilation unit text.

              In standard C++ available throughout the complete statically linked program
              is the most global a variable can be.

              In in-practice C++ it's possible for a variable to be even more global,
              exported to dynamically linked libraries.

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • kannansekar@gmail.com

                #8
                Re: global variables

                actually when you say,
                int variable;
                outside of all function , it means
                extern int variable; automatically which means, in another translation
                unit, I can declare
                extern int variable , and will be able to access the value,
                but
                static int variable; will not allow you to do that.
                But how that is done ?, every translation unit has something called
                "exported" symbols, in that
                'variable' will be included if you write ( int variable;) at the global
                level.

                Kannan Somasekar

                Comment

                • Donovan Rebbechi

                  #9
                  Re: global variables

                  On 2005-05-20, Victor Bazarov <v.Abazarov@com Acast.net> wrote:
                  [color=blue]
                  > Wrong. In C++ any function in an unnamed namespace still has external
                  > linkage by default. You give internal linkage to an object or a function
                  > if you declare it 'static' or 'const' without 'extern' (for objects).[/color]

                  Doh! So it does. I suppose there's a reason for this. When would one want/need
                  a function in an unnamed namespace to have external linkage ?

                  Cheers,
                  --
                  Donovan Rebbechi

                  Comment

                  • Alf P. Steinbach

                    #10
                    Re: global variables

                    * Donovan Rebbechi:[color=blue]
                    > On 2005-05-20, Victor Bazarov <v.Abazarov@com Acast.net> wrote:
                    >[color=green]
                    > > Wrong. In C++ any function in an unnamed namespace still has external
                    > > linkage by default. You give internal linkage to an object or a function
                    > > if you declare it 'static' or 'const' without 'extern' (for objects).[/color]
                    >
                    > Doh! So it does. I suppose there's a reason for this. When would one want/need
                    > a function in an unnamed namespace to have external linkage ?[/color]

                    One usage is as a template argument.

                    --
                    A: Because it messes up the order in which people normally read text.
                    Q: Why is it such a bad thing?
                    A: Top-posting.
                    Q: What is the most annoying thing on usenet and in e-mail?

                    Comment

                    Working...