Thread: Static variable initialization

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    55

    Static variable initialization

    Is this comment correct based on the code below: I was confused why var did not increment in each loop and why static_var did.

    Is this correct: In the context [in function] var = 5 and is incremented by 1, var = 6. Then for each loop var is re-initialized var = 5. While the static_var (since it is initialized only once) retains its incremented value during the loop process. static_var = 5, ++, loop, static_var = 6 ++, loop......

    Code:
    include <stdio.h>
     
    void function () { //An example function, with its own context
      int var = 5;
      Static int static_var = 5; // Static variable initialization
     
      printf("\t[in function] var = %d\n", var);
      printf("\t[in function] static var = %d\n", static_var);
      var++;                //increment var by 1
      static_var++;         //increment static_var by 1.
    }
     
    int main() {
      int i;
      static int static_var = 1337; //the main function, with its own context.
     
      for(i=0; i < 5; i++) {        //loop 5 times.
        printf("[in main] static_var = %d\n", static_var);
        function(); // Call the function
      }
    }

    [in main] static_var = 1337
    [in function] var = 5
    [in function] static var = 5
    [in main] static_var = 1337
    [in function] var = 5
    [in function] static var = 6
    [in main] static_var = 1337
    [in function] var = 5
    [in function] static var = 7
    [in main] static_var = 1337
    [in function] var = 5
    [in function] static var = 8
    [in main] static_var = 1337
    [in function] var = 5
    [in function] static var = 9
    Last edited by sjmp; 06-07-2013 at 12:59 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    There is an error on line 5.
    Otherwise what you say is correct.
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by sjmp View Post
    Then for each loop var is re-initialized var = 5.
    That is not correct. The function var is recreated each time the containing function is called, and ceases to exist when the function returns. Each time the function is called it has a different variable named var that is initialised to the value of 5. It is not the same variable every time, getting reinitialised.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by sjmp View Post
    Then for each loop var is re-initialized var = 5.
    Correct, but if a static variable is not specifically initialized, it's initialized to zero just one time.

    Code:
    #include <stdio.h>
    
    void foo(void);
    
    int main (void)
    {
    int i;
    
        for(i = 0; i < 10; i++)
            foo();
        return 0;
    }
    
    void foo(void)
    {
    static int j;
    
        printf("j = %d\n", j);
        j++;
    }
    output:

    j = 0
    j = 1
    j = 2
    j = 3
    j = 4
    j = 5
    j = 6
    j = 7
    j = 8
    j = 9

    A static variable is only allocated once when the program begins and deallocated when the program ends. Essentially, a static variable is a global variable with local scope (scope is source file or function), and not stored on the stack.

    Static (C++)
    Last edited by rcgldr; 06-07-2013 at 04:06 PM.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Grumpy - I appreciate the clarification. Do you know why every instance of the static variable var (in this instance) is assigned the same memory address? I do not understand how memory is allocated/distributed by the machine. Could be as simple as it was the last available address to assign, and then the first one available after the function returns.

    [in main] static_var @ 0x601040 = 1337
    [in function] var @ 0x7ffffdb024bc = 5
    [in function] static_var @ 0x601044 = 5
    [in main] static_var @ 0x601040 = 1337
    [in function] var @ 0x7ffffdb024bc = 5
    [in function] static_var @ 0x601044 = 6
    [in main] static_var @ 0x601040 = 1337
    [in function] var @ 0x7ffffdb024bc = 5
    [in function] static_var @ 0x601044 = 7
    [in main] static_var @ 0x601040 = 1337
    [in function] var @ 0x7ffffdb024bc = 5
    [in function] static_var @ 0x601044 = 8
    [in main] static_var @ 0x601040 = 1337
    [in function] var @ 0x7ffffdb024bc = 5
    [in function] static_var @ 0x601044 = 9

    Thanks
    Last edited by sjmp; 06-11-2013 at 12:46 PM.

  6. #6
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    rcgldr - when you say:
    but if a static variable is not specifically initialized, it's initialized to zero just one time.
    you mean for that specific instance/example only - because in this instance i = 0

    correct?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    No what rcgldr said is always the case, not just a special example. He was relaying a rule in the standard.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by sjmp View Post
    Do you know why every instance of the static variable var.
    There's only one instance of a static variable. It exist from the time the program starts until the program ends. It's normally stored in the same part of memory as global variables (called the data segment in some environments).

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by sjmp View Post
    Grumpy - I appreciate the clarification. Do you know why every instance of the static variable var (in this instance) is assigned the same memory address? I do not understand how memory is allocated/distributed by the machine.
    There is only one instance of the static variable static_var (in the function scope; there is a second static_var in the main scope which is a different variable with the same name).

    If you're asking why each of the 5 instances of var (which is not static) get the same address, that's not guaranteed to happen, however in this specific case it's very likely. Most compilers choose to use a call stack for local variables, function parameters, etc. In this particular case, the stack is in the same state before each of the successive calls to function(), and so function's stack frame gets created at the same location over and over. If the stack were in a different state (say, by calling a different function that ultimately called into this function) then the local var would most likely get a different address because the stack frame would be created at a different location in memory.

    All of that, by the way, is implementation defined. We talk about stack and heap but the standard doesn't guarantee there IS such a construct; however, it's very common that there is. Nothing about the C standard guarantees that var gets the same address every time through the loop (so you should absolutely never rely on that happening), but with how most implementations work, it's highly likely.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    55
    Makes sense. Thanks for the info.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static variable initialization
    By Saurabh Mehta in forum C Programming
    Replies: 8
    Last Post: 11-21-2012, 02:33 PM
  2. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  3. Why initialization of static member is a must?
    By meili100 in forum C++ Programming
    Replies: 9
    Last Post: 04-11-2008, 05:22 PM
  4. Replies: 6
    Last Post: 12-13-2007, 08:20 PM
  5. Order of initialization of static variables
    By Sang-drax in forum C++ Programming
    Replies: 4
    Last Post: 06-20-2004, 04:31 PM