Static local Variables

Source: Internet
Author: User

Adding the "Static" keyword to a local variable is a static local variable. Static local variables are stored in the global data area of the memory. At the end of the function, static local variables do not disappear, and each time the function is called, no space is redistributed for it. It always resides in the global data area until the program finishes running. The initialization of a static local variable is similar to a global variable. If it is not explicitly initialized, C + + automatically initializes it to 0.
A static local variable shares a global data area with a global variable, but a static local variable is visible only in the function that defines it. Static local variables and local variables in the storage location of different, so that the existence of the time limit is also different, resulting in the operation of both operations results are different.
For example, the following program defines global variables, static local variables, and local variables:

//*********************
* * Ch5_2.cpp * *
//*********************

#include <iostream.h>

void Func ();
int n=1; Global variables

void Main ()
{
Static

int A; Static local Variables
int b=-10; Local variables
cout << "A:" <<a
<< "B:" <<b
<< "N:" <<n <<endl;
b+=4;
Func ();
cout << "A:" <<a
<< "B:" <<b
<< "N:" <<n <<endl;
n+=10;
Func ();
}

void Func ()
{
Static

int a=2; Static local Variables
int b=5; Local variables
a+=2;
n+=12;
b+=5;
cout << "A:" <<a
<< "B:" <<b
<< "N:" <<n <<endl;
}

The result of the operation is:
   a:0 b:-10 N:l
A:4 B:10 N:13
a:0 b:-6 N:13
A:6 b:10 n:35

In the program main function main () two calls the Func () function, from the running results can be seen, the program control every time you enter the Func () function, the local variable B is initialized. While static local variable A is initialized only on the first call, and the second time it is entered, it is no longer initialized, and its value is the result value 4 after the first call. The variables A and B in the main () function are not the same as the variables A and B in the Func () function, so the corresponding values are different. Further discussion on scope and visibility of variables can be found in chapter 6th.
There are many uses for static local variables: You can use it to determine if a function has been called. Use it to keep the value of multiple calls.

Description of the static local variable:  
(1) A static local variable allocates a storage unit within a static storage area. is not released during the entire run of the program. Automatic variables (i.e. dynamic local variables) belong to the dynamic storage category, which is stored in dynamic storage space (not static storage space), and is released after the function call is finished. &NBSP;
(2) assigning the initial value to a static local variable is done at compile time, that is, it is assigned only one time, and it has the initial value when the program is running. The initial value is no longer re-assigned at each subsequent call to the function, but only at the end of the last function call. Instead of assigning an initial value to an automatic variable, not at compile time, but at the time of the function call, the function is given an initial value once per call, which is equivalent to executing an assignment statement. &NBSP;

(3) If you do not assign an initial value to a local variable, it is automatically assigned the initial value 0 (for a numeric variable) or a null character (to a character variable) for a static local variable. In the case of an automatic variable, if the initial value is not assigned, it is an indeterminate value. This is because the storage unit has been disposed after each function call, and the storage unit is reassigned again the next call, and the value in the allocated cell is indeterminate. &NBSP;
(4) Although a static local variable still exists after the function call ends, other functions cannot reference it, that is, it is "invisible" in other functions.

Transferred from: http://www.blogjava.net/faintbear/archive/2010/01/06/308408.html

Static local Variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.