Thread: scope of global variable vs. static

  1. #1
    Registered User
    Join Date
    Apr 2009

    scope of global variable vs. static

    Hi all,

    1) I had few questions regarding C++ static keyword. Is there any difference in scope between a in following two code snippets?

    const int a = 1;
    int main(...)
    const static int a = 1;
    int main(...)
    2) What about the similar situation with the variables replaced by functions? Would static keyword make any difference in scope?

    3) I also heard that in comparing declaring a const variable within any function versus declaring a static const variable in global scope, the latter is more efficient because it avoides stack allocation. Is it ture, and if so, where is the value stored?

    I tried to look it up in a C++ reference book I have, but I couldn't find it there or online. I'd appreciate for any pointers. Thanks!

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    RING 0
    in example 2, a is private across translation units. IE it is given static linkage opposed to the default extern linkage. Same with functions. Static can be confusing because it has a dual role in c and c++. It can be used to describe the lifetime or linkage of an object depending on where it is used.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    There is a distinction between scope and linkage. A file-static variable is global scope, but has internal linkage, meaning it is not visible to other modules at link time.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    If static is used inside a function, it means the variable won't ever "die": It is created once, and the next time the function is called it will remain the same, just as a global static variable private to the function.

    If static is used at top level, It means this variable is only visible in the file. So you can have two files "foo.c" and "bar.c" compiled together. They both have

    static int refcount(void) { ... }
    in them. The linker won't complain. But if you miss the static, it sees two global definitions of the same function and will put out an error.

    If static is used inside a class, it means the variable is shared among all instances of the class, so if 1 modifys it, the second will see it modified, too. If a function is static, this function can be called without an instance, such as

    2) The same for functions in your question.

    3) Yes. The former one allocates space every time the function is called and fills it with something. The value is stored just as a local variable. The latter one is putting a new variable into global space, which is initialized when the program starts.

    If I have any mistakes, please correct them.
    Last edited by Brafil; 06-20-2009 at 01:13 AM.

  5. #5
    Join Date
    Oct 2007
    Inside my computer
    No, 3) is not correct.
    The compiler does not have to push a const value onto the stack. If it's a constant expression, the compiler can just ignore it (and replace every instance of it with its value).
    There is a very good chance that any simple const types will simply be optimized away (ie no storage).
    This applies to both local and global. If it did allocate it everytime, then obviously the global one would be faster. This might apply more to non-const variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static variable in templates
    By arjunajay in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 08:44 AM
  2. filling a static member map variable
    By elad in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2004, 12:43 PM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM