C ++ object-oriented - the third job

Job Title: Mu lesson to learn in Chapter 5. And choose the one you think meaningful knowledge or difficulties to run code on the computer test, write a complete experimental verification process. Experimentally proved that the use of the knowledge points.



Analysis of variable life cycle

First, the definition of the life cycle: the variables when the program runs the existence of time.

Second, the classification:

1, global variables

There has been a program is running.

2, global static variables

There has been a program is running.

3, local variables

When the program runs out of local scope, variables disappeared, to be precise destruction, hey.

4, static local variable

There has been a program is running. (This understanding before and I wrote this article seems not the same, here I am clear)

Third, the process of verification

Look at the following piece of code:

#include <iostream>

using namespace std;
int entire_a = 1;//赋了初值的全局变量
int entire_b;//全局变量
static int entire_c;//全局静态变量

void Local1()//验证静态局部变量只在第一次进入的时候做初始化,以后会跳过初始化语句,保留原来的值
{
    int i = 10;
    while (i <= 20)
    {
        static int n = 1;//静态局部变量
        n += 1;
        static int local_t = 2;
        i += local_t;
    }
}
void Local2()
{
    int local_s = 6;//局部变量
    cout << local_s;
    static int local_t = 3;//静态局部变量
    cout << "local_t = " << local_t << ",local_s = " << local_s << endl;
}

int main()
{
    Local1();
    cout << "a = " << entire_a << ",b = " << entire_b << ",c = " << entire_c << endl;//输出全局变量的初始值
    entire_a = 11;//改变全局变量a的值
    entire_b = 121;//改变全局变量b的值
    entire_c = 131;//改变全局静态变量c的值
    cout << "a = " << entire_a << ",b = " << entire_b << ",c = " << entire_c << endl;//输出修改之后之后的全局变量值

    Local2();//调用函数Local(),输出局部变量local_s的值和局部静态变量local_t的值
    int local_s = 66;//重新定义的新的局部变量local_s
    static int local_t = 33;//重新定义的新的局部变量local_t
    cout << "local_t = " << local_t << ",local_s = " << local_s << endl;//可以输出新的局部变量local_s和新的局部变量local_t
 }


As can be seen a, b, c have been present in the global, i.e. allocate memory and initialize the global object compile time, but the time and local_t local_s used in the main function must redefine (corresponding to regenerate a new variable, not before a) , or is this a problem below

Static local variable declarations :

void Local1()//验证静态局部变量只在第一次进入的时候做初始化,以后会跳过初始化语句,保留原来的值
{
    int i = 10;
    while (i <= 20)
    {
        static int n = 0;//静态局部变量
        n += 1;
        static int local_t = 2;
        i += local_t;
    }
}




As can be seen from these three figures initialized n = 0, the first time after being skipped, can be drawn: a static local variable is equivalent to a global variable (but can only access this function, but the life cycle and global variables about the same), after the function exits variables still, but only at first to do the initial time of entry, the future will skip the initialization statement, retain the original value.

CONCLUSIONS:

1, variable scope is different from the life cycle. Scope and life cycle attributes of variables in space and time reflects two dimensions. Scope is a range of variables that can be referenced, and the life cycle is a variable that can be referenced period of time. There are many ways in C ++ to specify the scope and lifetime of a variable. The most common, such as: {}, static modifier like.
2, stored in the variable region of global data life cycle exists in the entire program, but the data stored in the stack with the end of the scope of functions, and so lead to the destruction of the stack and local variables in addition to static variables are stored in the stack. Whether in the global variable area of the global or local declaration of static variables are stored in the program. Therefore, a static local variable life cycle has been present during the program run .

Guess you like

Origin www.cnblogs.com/xiaonannanbuchengxian/p/11610293.html