CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    May 2005
    Posts
    112

    Question local static variables

    hi,

    is it correct to state that a static variable is created only once and initialised only once and occupies some special global memory?

    So lets say some sub routines declare and initialise a local static variable of the same name.

    Is this possible - I mean will the variables only have local scope to the sub routines they are declared and initialised in ?
    Will they exist seperately or how are they allocated?


    Code:
    void my_func1 ()
    {
    	static int var = 0;
                    // do whatever
    }
    
    void my_func2 ()
    {
    	static int var = 0;
                    // do whatever
    }
    
    
    int main ()
    {
    	my_func1();
                    my_func2();
    	return 0;
    }
    Last edited by Moore; October 18th, 2005 at 12:52 PM.

  2. #2
    Join Date
    May 2004
    Posts
    28

    Re: local static variables


    life of a static variable declared in a function begins at the time of 1st execution of the function and till the program ends. Yes it's allocated only once and contains the value which was assigned to it in that function while the program is running. In your example, these are two different variables, and they have different addresses in memory. And they are seen only in the functions they are declared in.

    Hope this helps some,
    Oleg
    Last edited by Xatrix; October 18th, 2005 at 01:38 PM.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: local static variables

    Just to be clear, a static local is only initialised once - when it's created. If you change its value in the function, it will retain that value on the next call.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: local static variables

    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  5. #5
    Join Date
    May 2005
    Posts
    112

    Re: local static variables

    ok, thanks alot everyone .

  6. #6
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: local static variables

    Quote Originally Posted by Moore
    hi,

    is it correct to state that a static variable is created only once and initialised only once and occupies some special global memory?

    So lets say some sub routines declare and initialise a local static variable of the same name.

    Is this possible - I mean will the variables only have local scope to the sub routines they are declared and initialised in ?
    Will they exist seperately or how are they allocated?


    Code:
     
    void my_func1 ()
    {
    	static int var = 0;
    // do whatever
    }
     
    void my_func2 ()
    {
    	static int var = 0;
    // do whatever
    }
     
     
    int main ()
    {
    	my_func1();
    my_func2();
    	return 0;
    }
    as Graham and Xatrix explained you very well.in your Program u r using two static variable local to the function.
    First something regarding static
    In both C and C++ the keyword static has two basic meanings, which
    unfortunately often step on each other’s toes:


    Allocated once at a fixed address; that is, the object is created in a special
    static data area rather than on the stack each time a function is called. This is
    the concept of static storage.

    Local to a particular translation unit (and local to a class scope in C++, as
    you will see later). Here, static controls the visibility of a name, so that
    name cannot be seen outside the translation unit or class. This also
    describes the concept of linkage, which determines what names the linker
    will see.

    According to your Program==>>
    When you create a local variable inside a function, the compiler allocates
    storage for that variable each time the function is called by moving the stack
    pointer down an appropriate amount. If there is an initializer for the variable,
    the initialization is performed each time that sequence point is passed.



    Sometimes, however, you want to retain a value between function calls. You
    could accomplish this by making a global variable, but then that variable
    would not be under the sole control of the function. C and C++ allow you to
    create a static object inside a function; the storage for this object is not on the
    stack but instead in the program’s static data area. This object is initialized
    only once, the first time the function is called, and then retains its value
    between function invocations.
    Last edited by humptydumpty; October 19th, 2005 at 01:10 AM.

  7. #7
    Join Date
    May 2005
    Posts
    112

    Re: local static variables

    hi,

    According to your Program==>>
    When you create a local variable inside a function, the compiler allocates
    storage for that variable each time the function is called by moving the stack
    pointer down an appropriate amount. If there is an initializer for the variable,
    the initialization is performed each time that sequence point is passed.

    So this is a little confusing as it states that the "initialisation is performed each time that sequence point is passed"

    so considering the sample program again, are you saying that each time the my_func 's are called, that new storage is allocated and init to 0 is done again? If so how can the "var" retain its value?

    thanks.

  8. #8
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: local static variables

    Quote Originally Posted by Moore
    hi,
    So this is a little confusing as it states that the "initialisation is performed each time that sequence point is passed"
    so considering the sample program again, are you saying that each time the my_func 's are called, that new storage is allocated and init to 0 is done again? If so how can the "var" retain its value?
    thanks.
    Nope In case of static variable only Singel TIme Initialization take Place .

    have u read this

    C and C++ allow you to create a static object inside a function; the storage for this object is not on the stack but instead in the program’s static data area. This object is initialized only once, the first time the function is called, and then retains its value between function invocations.

    In Upper Quote i mean to say if this is a Auto variable

    have a look on This
    According to your Program==>>
    When you create a local variable inside a function, the compiler allocates storage for that variable each time the function is called by moving the stack pointer down an appropriate amount. If there is an initializer for the variable, the initialization is performed each time that sequence point is passed.
    Last edited by humptydumpty; October 19th, 2005 at 01:26 AM.

  9. #9
    Join Date
    May 2005
    Posts
    112

    Re: local static variables

    yes and that is my understanding, but I thought that the paragragh I quoted from your previous post was a little contradictory to the highlighted red text.

    regardless, I pretty much understand it now.

    thanks.

  10. #10
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: local static variables

    Quote Originally Posted by Moore
    yes and that is my understanding, but I thought that the paragragh I quoted from your previous post was a little contradictory to the highlighted red text.

    regardless, I pretty much understand it now.

    thanks.
    u welcome. and sorry to take second time to understand

  11. #11
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: local static variables

    Quote Originally Posted by Moore
    So lets say some sub routines declare and initialise a local static variable of the same name. Is this possible - I mean will the variables only have local scope to the sub routines they are declared and initialised in ? Will they exist seperately?
    I guess that rest of your concerns have been answered. About the existence of two static variables with same name in two different functions in the same translation unit, probably there is some kind of name-mangling to ensure independent and unique existence but I am not sure about how compilers do this actually. And I would be little concerned about it since the compiler does this job for me very well. Hope this helps. Regards.

  12. #12
    Join Date
    May 2005
    Posts
    112

    Re: local static variables

    great, thanks again to all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured