non-static global variables and the extern qualifier

Discussion in 'C++' started by A, Nov 26, 2003.

  1. A

    A Guest

    Hi,

    Consider this code:

    //Header File - Foo.h

    int i = 0; // non-static global variable

    class Foo{
    ...
    };


    The scope of i is throughout the entire program since it is global and
    non-static. Lets say i want to assign a new value to i in another file
    called Bar.h, how is it done?

    //Header File - Bar.h

    i = 1 //like this? or must i use the extern qualifier here? confusion here
    is that it i is global then we u need extern to make it external to another
    file when it should be already available?

    class Bar{
    ...
    };


    Regards,
    A
     
    A, Nov 26, 2003
    #1
    1. Advertising

  2. A

    Rolf Magnus Guest

    A wrote:

    > Hi,
    >
    > Consider this code:
    >
    > //Header File - Foo.h
    >
    > int i = 0; // non-static global variable
    >
    > class Foo{
    > ...
    > };
    >
    >
    > The scope of i is throughout the entire program since it is global and
    > non-static.


    Right, but you defined i in a header, and if you #include that header in
    more than one translation unit, you'll get a linker error for multiple
    definitions. Rather define it in the .cpp file, and just declare it in
    the header.

    > Lets say i want to assign a new value to i in another file
    > called Bar.h, how is it done?
    >
    > //Header File - Bar.h
    >
    > i = 1 //like this?


    Well, not in the global scope, but within a function, yes.

    > or must i use the extern qualifier here?


    No, but you must have declared it extern.

    > confusion
    > here is that it i is global then we u need extern to make it external
    > to another file when it should be already available?


    You should do it something like this:

    Foo.cpp:

    int i = 0;
    //...

    Foo.h:

    extern int i;

    Bar.cpp:

    #include "Foo.h"

    void somefunc()
    {
    i = 1;
    }

    i is defined in Foo.cpp and is global, but other .cpp files like Bar.cpp
    don't know anything about it. Just like you have to make a type or a
    function from another tranlation unit known, you have to make a
    variable known. The "extern int i" tells the compiler that there is a
    global variable i of type int that defined elsewhere.
     
    Rolf Magnus, Nov 26, 2003
    #2
    1. Advertising

  3. A

    A Guest


    > A wrote:
    >
    > > Hi,
    > >
    > > Consider this code:
    > >
    > > //Header File - Foo.h
    > >
    > > int i = 0; // non-static global variable
    > >
    > > class Foo{
    > > ...
    > > };
    > >
    > >
    > > The scope of i is throughout the entire program since it is global and
    > > non-static.

    >
    > Right, but you defined i in a header, and if you #include that header in
    > more than one translation unit, you'll get a linker error for multiple
    > definitions. Rather define it in the .cpp file, and just declare it in
    > the header.


    Firstly, i agree with the error.

    #include would obviously bring i into scope wherever you included it, and
    thus defeats the purpose of the external qualifier? I gather from what Ive
    read that by using the external qualifer you can open the scope to multiple
    files - it does not make mention of also using #include.

    I was thinking more on the lines:

    Foo.h

    int i = 0; // non-static global variable

    class Foo{
    ...
    };

    Bar.h

    extern int i; //refers to the i declared in Foo.h; no need to use #include

    class Bar{
    ...
    };


    Regards,
    A
     
    A, Nov 26, 2003
    #3
  4. A

    Rolf Magnus Guest

    A wrote:

    >
    >
    >> A wrote:
    >>
    >> > Hi,
    >> >
    >> > Consider this code:
    >> >
    >> > //Header File - Foo.h
    >> >
    >> > int i = 0; // non-static global variable
    >> >
    >> > class Foo{
    >> > ...
    >> > };
    >> >
    >> >
    >> > The scope of i is throughout the entire program since it is global
    >> > and non-static.

    >>
    >> Right, but you defined i in a header, and if you #include that header
    >> in more than one translation unit, you'll get a linker error for
    >> multiple definitions. Rather define it in the .cpp file, and just
    >> declare it in the header.

    >
    > Firstly, i agree with the error.
    >
    > #include would obviously bring i into scope wherever you included it,
    > and thus defeats the purpose of the external qualifier?


    Why?
    If you write:

    int i = 0;

    it lets the compiler know there is an int named i _and_ that it should
    allocate space for that int.

    extern int i;

    also lets the compiler know there is an int named i, but it does _not_
    allocate storage for it. So the extern just means "there is a variable,
    but it alredy exists somewhere else". The linker will resolve it then.

    > I gather from what Ive read that by using the external qualifer you
    > can open the scope to multiple files - it does not make mention of
    > also using #include.
    > >

    > I was thinking more on the lines:
    >
    > Foo.h
    >
    > int i = 0; // non-static global variable
    >
    > class Foo{
    > ...
    > };


    Well, this would only be ok as long as you #include "Foo.h" only _once_
    in your whole program. But that would defeat the purpose of headers.

    > Bar.h
    >
    > extern int i; //refers to the i declared in Foo.h; no need to use
    > #include


    Right. But headers are just there to make things from one translation
    unit known to another one. You put it all in a header so that you don't
    need to repeat everything wherever it is needed. And just as you would
    put a function declaration in the header and define it in the .cpp
    file, you should only declare your global variables (and that's what
    the extern actually means) in the header and put the definition in
    the .cpp file.

    > class Bar{
    > ...
    > };
     
    Rolf Magnus, Nov 27, 2003
    #4
  5. A

    Jack Klein Guest

    On Wed, 26 Nov 2003 21:32:53 +1030, "A" <> wrote in
    comp.lang.c++:

    > Hi,
    >
    > Consider this code:
    >
    > //Header File - Foo.h
    >
    > int i = 0; // non-static global variable


    Actually, i is defined without the static keyword, but it is indeed a
    static object, meaning that is has static storage duration.

    > class Foo{
    > ...
    > };
    >
    >
    > The scope of i is throughout the entire program since it is global and


    No, you are confusing scope with linkage. The definition of i has
    namespace scope in C++, and since it is at top level in the global
    namespace, it is basically the same as what is called "file scope" in
    C, even though that terminology is no longer used in C++.

    The definition is in scope from the point where it appears in the
    translation unit until the end of the translation unit, although it
    may be hidden by another usage of the same name in an inner block.

    > non-static. Lets say i want to assign a new value to i in another file
    > called Bar.h, how is it done?


    You can only assign a value to an object in code. You can only
    initialize an object in its definition. So if i is defined in a
    source file, you can't initialize it anywhere else, and you must
    assign to it from inside a function, not at file level.

    > //Header File - Bar.h
    >
    > i = 1 //like this? or must i use the extern qualifier here? confusion here
    > is that it i is global then we u need extern to make it external to another
    > file when it should be already available?
    >
    > class Bar{
    > ...
    > };
    >
    >
    > Regards,
    > A


    You have some confusion over three separate but somewhat related
    concepts, scope, linkage, and storage duration. Part of the confusion
    stems from the fact that C++ inherited two different meanings of the
    "static" keyword from C, not to mention adding another meaning of its
    own.

    Any object defined outside of a function has static storage duration,
    which means it exists for the life of your program. Adding the
    keyword "static" or the keyword "const" to such an object does not
    change the storage duration, which is still static, and does not
    change the scope, which is still to the end of the translation unit,
    it merely changes the linkage from internal to external.

    External linkage means that code in another translation unit may refer
    to the object by name, provided that that code has a suitable external
    declaration in its scope.

    And the definition of translation unit is basically the source file
    you pass to the compiler plus everything it includes.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Nov 27, 2003
    #5
  6. "Jack Klein" <> wrote...
    > [...]
    > Any object defined outside of a function has static storage duration,
    > which means it exists for the life of your program. Adding the
    > keyword "static" or the keyword "const" to such an object does not
    > change the storage duration, which is still static, and does not
    > change the scope, which is still to the end of the translation unit,
    > it merely changes the linkage from internal to external.


    Vice versa, actually. "static" changes linkage from external to internal.

    Victor
     
    Victor Bazarov, Nov 27, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ±ÇÌéÍõ×Ó
    Replies:
    7
    Views:
    809
  2. MikeF

    EXTERN Qualifier

    MikeF, Apr 14, 2006, in forum: C Programming
    Replies:
    7
    Views:
    341
  3. StephQ
    Replies:
    18
    Views:
    979
  4. Andre
    Replies:
    5
    Views:
    542
    Keith Thompson
    Jul 17, 2012
  5. rickman
    Replies:
    5
    Views:
    428
    rickman
    Mar 30, 2013
Loading...

Share This Page