Thread: static variable scope

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    static variable scope

    Hello.

    Code:
    void Blah()
    {
        static int i;
        ...
        if(case)
        {
            static int i;
            ...
        }
    }
    I cannot find the definition on how this is handled.
    Apparently the VC 2003 compiler handles this differently compared to the 2008 version.

    Thanks a lot for you help!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The innermost int i should hide the outermost int i, so that the outermost variable is not visible in the if-case. I don't see anything about how static would make those two variables the same.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Quote Originally Posted by tabstop View Post
    The innermost int i should hide the outermost int i, so that the outermost variable is not visible in the if-case. I don't see anything about how static would make those two variables the same.
    "Should" is too vague i fear.
    The two mentioned compiler handle it differently as I have said.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, "shall" if you want to be more formal about it. The standard says this:
    Quote Originally Posted by ISO C++ 3.3, para 1
    Every name is introduced in some portion of program text called a declarative region, which is the largest part of the program in which that name is valid, that is, in which that name may be used as an unqualified name to refer to the same entity. In general, each particular name is valid only within some possibly discontiguous portion of program text called its scope. To determine the scope of a declaration, it is sometimes convenient to refer to the potential scope of a declaration. The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name. In that case,
    the potential scope of the declaration in the inner (contained) declarative region is excluded from the scope of the declaration in the outer (containing) declarative region.
    Since there are no exceptions listed for static-linkage variables, I would see no reason for them to be handled differently.

    Edit: I said static-linkage; I mean static duration. Static variables at block scope still have no linkage (static only specifies external linkage at namespace scope according to 3.5).
    Last edited by tabstop; 10-28-2008 at 04:33 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Okay.

    I remember to have heard something about the MS compilers not following the standard with variable scopes.
    Does anyone know about this particular case with static variables (using max compiler/linker optimizations)?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Does anyone know about this particular case with static variables (using max compiler/linker optimizations)?
    So are you saying that one of them is wrong, based on the quote from the standard provided by tabstop?

    Are they consistent with the standard (or each other) at lower optimisation settings?

    Are they consistent with the standard (or each other) for non-static variables?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sqrt(-1) View Post
    Okay.

    I remember to have heard something about the MS compilers not following the standard with variable scopes.
    Does anyone know about this particular case with static variables (using max compiler/linker optimizations)?
    Older versions are less standards compliant, yes. As is it with any compiler.
    Therefore, use as new version as you possibly can.
    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 usage
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 12:33 AM
  2. Replies: 20
    Last Post: 01-26-2009, 01:33 AM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  4. static variable
    By George2 in forum C Programming
    Replies: 2
    Last Post: 08-24-2007, 07:33 AM
  5. Replies: 3
    Last Post: 10-10-2002, 07:34 AM