The function of the static variable:

Source: Internet
Author: User

concept: static variables are scoped to a file , the program starts with space allocated, the space is freed at the end, the default is initialized to 0, and its value can be changed when used. static variables or static functions only the code in this file can access it , and its name is not visible in other files.

1. The first and most important one is to introduce it: hide All global variables and functions that do not have a static prefix have global visibility when we compile multiple files at the same time.

2. The second function of static is to keep the variable content persisted in the static data area of the variable will be initialized when the program starts running, and is the only one initialization. A total of two variables are stored in static storage: Global variables and static variables, except that, compared to global variables, static can control the visible range of variables, in the final analysis, static is used to hide them.

3. The third effect of static is initialized to 0 by default. In fact, global variables also have this property, because global variables are also stored in the static data area

4. Summary of functions and variables declared with static

Static declared variables have two characteristics in the C language:

1), the variable will be placed in the program's global store, so that the next call can also maintain the original assignment. This is the difference between a stack variable and a heap variable.

2), variable with static to tell the compiler, itself only within the scope of the variable is visible. This is the difference between it and the global variable .

Differences between global variables, static global variables, static local variables, and local variables

Variables can be divided into: global variables, static global variables, static local variables, and local variables .

(1) According to the storage area, global variables, static global variables and static local variables are stored in the static storage area of memory , local variables are stored in the memory stack area .

(2) by scope, Global variables are valid throughout the project file ; a static global variable is only valid within the file in which it is defined ; a static local variable is only valid within the function that defines it , except that the program allocates only one memory, and after the function returns, The variable does not disappear , and the local variable is valid within the function that defines it, but the function returns and then fails .

A static global variable is formed by the description of the global variable (external variable), preceded by static. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the entire source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.

From the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.

(1) The static function is different from the normal function scope. Only in this document. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to which the function is to be used is to contain the header file

(2) What is the difference between a static global variable and a normal global variable: Thestatic global variable is initialized only once, preventing it from being referenced in other file units;

(3) What is the difference between static local variable and ordinary local variable:static local variable is initialized only once, next time according to last result value;
(4) What is the difference between a static function and a normal function: The static function hasonly one copy in memory, and the normal function maintains a copy of each call.
(5) Global variables and static variables are initialized to 0 by the compiler if they are not manually initialized . The value of the local variable is not known.

5. The static of C + +

The dynamic data generated by new in the general program is stored in the heap area, and the automatic variables inside the function are stored in the stack area. Automatic variables generally free up space as the function exits, and static data (even static local variables inside the function) is stored in the global data area. The data in the global data area does not free up space because of the function's exit.

1) Static global variables cannot be used by other files;

2) variables of the same name can be defined in other files, and no conflict will occur;

Typically, a variable is defined in the body of the function that allocates stack memory to the local variable whenever the program runs to the statement. However, as the program exits the function body, the system will retract the stack memory, and the local variables are invalidated accordingly. But sometimes we need to save the values of the variables between the two calls. The usual idea is to define a global variable to implement. In this way, the variables are no longer part of the function itself, and are no longer only controlled by the functions, which inconvenience the maintenance of the program. Static local variables can solve this problem. Static local variables are saved in the global data area instead of being saved in the stack, and each time the value is persisted to the next call.

The function of the static variable:

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.