当前位置:网站首页>Cold knowledge of C language 03_ Scope and life cycle, self-cultivation of variables

Cold knowledge of C language 03_ Scope and life cycle, self-cultivation of variables

2021-06-22 00:27:04 Invincible little cheap fa

01 - The scope and life cycle of variables

In the last article, we looked at variables from the perspective of God , See through the nature of variables , It's a definition 、 Meaningful and variable memory , It's just her grammar and semantics , Put it into programming , She's a programmer who creates and invokes , Since I can play an important role in the code world , Then she must have her own charm , Scope and life cycle is her self-cultivation . Self cultivation of variables is very important , It's about the design of the code , Old rules , Let's start with a few questions :

1、 Beyond scope , What is the role of the life cycle ?

2、 Can a program know that the life cycle of a variable is over ?

3、 Life cycle is dynamic or static ?

To understand these problems , You have to know what the nature of scope and lifecycle is , Know the essence to solve the problem .

1.png

02 - Scope of variable

Scope : You can access the region of the variable through the variable name

Notice the scope The emphasis must be on accessing through variable names , A pointer to a variable does not count , It's hard to understand ? for instance :

int* IncAndGetVar(void)
{
    static int Num = 0;
    Num++;
    return &Num;
}

void Func(void)
{
    int* pVar = IncAndGetVar();
    *pVar += 2;
}
 Copy code 

In the above code , Variable Num The scope of is just IncAndGetVar Internal function , stay Func() There is no Num Scope of action . because Num yes IncAndGetVar() Static local variables of functions ,Num The variable name is only visible inside the function , therefore Num The scope of IncAndGetVar() Within the function , Even if Func() Function gets Num And changed Num Value , But not with the name of the variable itself , So beyond IncAndGetVar(), It goes beyond variables Num Scope of action . Here's a conclusion : Out of scope , You can also access variables ( Through the address ), The premise is that the life cycle still exists .

2.png

After knowing the essence , About what global static 、 Local static 、 Do you need to memorize the scope of global automatic variables ? I don't want it any more , You can deduce the scope by looking at the name :

Global automatic variables : As long as the variable is declared (extern xxx) And the file that defines it does not exist in the local variable area with the same name , It's all about its scope , It can be understood as all areas of the project , If the global and local names are the same , The local operation will mask the global

Global static variables : Only in the file that defines it and there is no local variable area with the same name , Because of static reasons , Its variable name is not visible in other files , At the same time, if the global and local names are the same , The local operation will mask the global ……

03 - Life cycle of variable

Life span : The time period in which this variable can be accessed

Pay attention to the life cycle The emphasis is to be able to access , Visit yes 2 Kind of ,1 Is a variable name. ,2 Is a pointer , As long as one of them is accessible and valid , So the life cycle exists , So we can judge whether the life cycle of the variable exists , It depends on whether it's valid in memory , Or the example above :

int* IncAndGetVar(void)
{
    static int Num = 0;
    Num++;
    
    return &Num;
}

void Func(void)
{
    int* pVar = IncAndGetVar();
    *pVar += 2;
}
 Copy code 

Variable Num The life cycle of a program exists throughout the running phase of the program , Because it's a local static variable , As long as the program is running , It's in memory , Valid in memory , There's a way to access it . Variable pVar The life cycle of Func() Inside the function , Because it's a local automatic variable , function Fcun After call ,pVar Will be released by the compiler , Once released , This variable has no place in memory , It's invalid , When a variable is invalid in memory , And the life cycle comes to an end .

Then there's a question , If Num It's a local automatic variable ? It's down there :

int* IncAndGetVar(void)
{
    int Num = 0;
    Num++;
    
    return &Num;
}

void Func(void)
{
    int* pVar = IncAndGetVar();
    *pVar += 2;
}
 Copy code 

I learned this way C The pointer estimates all know , In this way, once the program runs, it may crash , Because the local automatic variable is at the end of the function call , Its life cycle is over , The place it would have occupied would have been occupied by other people , That is, it is invalid in memory , You can't visit it anymore .

04 - Question answering

1、 Beyond scope , What is the role of the life cycle ?

Beyond scope , If the life cycle still exists , The role of life cycle is to make variables always valid in memory , Waiting to be interviewed , It can't go away , It's like standing in the manger and not taking a shit .

2、 Can a program know that the life cycle of a variable is over ?

Can't know , Because it's impossible to know if the memory is valid .

3、 Life cycle is dynamic or static ?

Is dynamic , When a function is called , The life cycle of local variables exists , At the end of the function , The life cycle of local variables disappears .

版权声明
本文为[Invincible little cheap fa]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/06/20210601194402350v.html