Thread: Doubt regarding storage of a static variable

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    53

    Doubt regarding storage of a static variable

    Hi frnds.. please help me in solving my doubt

    In the below prog

    Code:
    #include.......
    static int x = 10;
    
    main()
    {
        fun1();
        sleep(1);
         fun1();
         fun2();
    }
    
    fun1(){
      static int x = 11;
      printf("%d \n", x++ );
    }
    
    fun2()
    {
      printf( "%d \n", x );
    }

    we have static var x globally initialized, and the static var with same name x is defined and modified with block scope in fun1 and the static var x being used in fun2.

    Actually static variables will be stored in BSS (if uninitialized) and in Data seg (if initialized). But in the above prog where the locally defined static var x in fun1 is stored?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by karthik537
    Actually static variables will be stored in BSS (if uninitialized) and in Data seg (if initialized).
    Actually, that is implementation defined.

    Quote Originally Posted by karthik537
    But in the above prog where the locally defined static var x in fun1 is stored?
    The difference is a matter of scope; both variables have static storage duration and are explicitly initialised. With that in mind, you can figure out what is likely to happen, given what you already know.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Thanks a lot for your answer. Conceptually.... yes the scope varies.. so, static var in fun1() will be stored in Data Segment with the block scope .... and even the global var x will also be stored in Data segment but with file scope. If so, when x is getting incremented in fun1() technically how the program will know which x should get incremented?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Frnds.... help needed

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by karthik537
    yes the scope varies.. so, static var in fun1() will be stored in Data Segment with the block scope .... and even the global var x will also be stored in Data segment but with file scope
    Why not write out your program properly, compile and check if your hypothesis holds true for the given implementation? objdump could help here.

    Quote Originally Posted by karthik537
    If so, when x is getting incremented in fun1() technically how the program will know which x should get incremented?
    The rules of scope dictate which x will get incremented.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by karthik537 View Post
    Thanks a lot for your answer. Conceptually.... yes the scope varies.. so, static var in fun1() will be stored in Data Segment with the block scope .... and even the global var x will also be stored in Data segment but with file scope. If so, when x is getting incremented in fun1() technically how the program will know which x should get incremented?
    Both x's will be stored in the data segment, but that's their storage class and is very different from their scope.

    The C lang isn't block structured except when it comes to variables and any variable defined in a block masks out a same name variable outside that block. So, the x in fun1() masks out the global x, only when fun1() is entered, elsewhere the global x has visibility.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The keyword 'static' means something totally different when used with global variables vs. inside a function.
    When used on a global variable it means that the variable is only visible inside this source module. It will not interfere with any global variables of same name in other source modules.
    When used on a variable inside a function it means that the value of the variable will remain. That is, re-entering the function will find the value is still there. Basically it ensures it is not on that stack but is instead in stable data space.

    As itCbitC stated, type of physical memory used is less important than variable accessibility regarding C scoping rules.

    Your question: "... technically how the program will know which x should get incremented?" is answered by realizing that the compiler generates memory access instructions according to the context in which it finds the variables. When inside a function, references to variables defined therein are made to wherever they are physically stored. The compiler is not confused just because the variable's name is also found elsewhere in the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static storage duration question.
    By nimitzhunter in forum C Programming
    Replies: 5
    Last Post: 08-10-2010, 01:18 PM
  2. static storage class
    By roaan in forum C Programming
    Replies: 4
    Last Post: 09-08-2009, 04:57 PM
  3. Doubt on usage of static...
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 05-11-2009, 08:09 AM
  4. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  5. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM