当前位置:网站首页>Global variables & local variables

Global variables & local variables

2022-06-25 07:19:00 Xinchen sea

Scope :

    Global variables : Its scope of action is “ The whole project ”, Just define... In one source file , It works on all source files . Of course , Other source files that do not contain global variable definitions need extern Keyword declares the global variable again
    Static global variables : Use static Keyword modification , It also has global function , The difference from global variables is that if the program contains multiple files , Its scope is only in the defined file , Cannot work on other files , So even if two different source files define static global variables with the same name , They are also different variables
    local variable : Just start at the defined position , To the right curly bracket that defines it , Exists only during function execution , After the execution of a call to the function , Variables are revoked , The memory it uses is also reclaimed
    Static local variables : Local scope , It is initialized only once , It's been around since the first initialization until the end of the program , The difference with local variables is that after the function is executed, it still exists

Life span :

    Global variables : As the process goes on
    Static global variables : As the process goes on
    local variable : From the beginning of the definition to the end of the function , The variable is undone after the function is called , Memory is recycled
    Static local variables : As the process goes on ,static The lifetime of a modified local variable changes from a function to the entire process

Memory allocation :

    Global variables : overall situation ( static state ) Storage area
    Static global variables : overall situation ( static state ) Storage area
    local variable : Store in a stack , Dynamically allocate storage units to variables only when the function is called
    Static local variables : overall situation ( static state ) Storage area

Other :

    overall situation ( static state ) Storage area : It is divided into DATA Paragraph and BSS paragraph .DATA paragraph ( Global initialization area ) Store initialized global variables and static variables ;BSS paragraph ( Global uninitialized area ) Store uninitialized global variables and static variables . Release automatically at the end of the program . among BBS Segments are automatically cleared by the system before the program is executed 0, So the uninitialized global variables and static variables have been changed to 0
    Static variables are placed in the program's static data store ( The whole picture can be seen ) in , In this way, the original assignment can be kept at the next call . This is the difference between stack variable and heap variable
    Variable usage static Tell the compiler , I am only visible in the range of variables . This is the difference between it and global variables
    Changing a local variable to a static variable changes its storage mode, that is, its lifetime . Changing a global variable to a static variable changes its scope , It limits its use . therefore static The function of this specifier is different in different places ;
 

原网站

版权声明
本文为[Xinchen sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250454069896.html