C + + language variable problems (global variables, local variables, static global variables, static local variables)

Source: Internet
Author: User

The 1.c/c++ variable has different scopes according to the different life cycles of the defined positions, and the scope can be divided into 6 types:

Global scopes, local scopes, statement scopes, class scopes, namespace scopes, and file scopes.

From the scope view:

1> Global variables have global scope. A global variable can be used for all source files simply by defining it in one source file. Of course, other source files that do not contain global variable definitions need to declare the global variable again with the extern keyword.

2> static local variable has a local scope, it is initialized only once, since the first time it was initialized until the end of the program run, it differs from the global variable is that the global variable is visible to all functions, whereas the static local variable is always visible to the function body that defines itself ( That is, its scope is only within the {} that defines it, and if there is an identical variable name outside of {}, it is irrelevant.

3> Local variables are also only local scope, it is an automatic object (auto) (this is the default variable type), it does not persist during the program run, but only during the execution of the function, the function after the execution of a call, the variable is revoked, and its memory is taken back.

4> Static global variables also have global scope, which differs from global variables in that if a program contains multiple files, it acts on the file that defines it and does not work in other files, that is, variables modified by the static keyword have a file scope. This way, even if two different source files define static global variables of the same name, they are also different variables.

2. From allocating memory space, see:
1> global variables, static local variables, static global variables allocate space in static storage, and local variables allocate space in stacks

The 2> global variable itself is 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 whole 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.

(1) Static variables are placed in the program's static data store (globally visible) so that the original assignment can be maintained at the next call. This is the difference between a stack variable and a heap variable.
(2) Variables are used to tell the compiler that they are only visible within the scope of the variable. This is the difference between it and the global variable.

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. So the function of the static descriptor is different in different places. Should be taken into consideration.

A. If the global variable is accessed only in a single C file, you can modify the variable to a static global variable to reduce the coupling between the modules;

B. If the global variable is accessed only by a single function, the variable can be changed to a static local variable of the function to reduce the coupling between the modules;

C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider the reentrant problem, because they are all placed in a static data store and are globally visible;

D. If we need a reentrant function, then we must avoid using the static variable in the function (a function called "Internal memory" function)

The static variable condition must be used in the function: for example, when the return value of a function is a pointer type, it must be the address of the static local variable as the return value, and if it is the auto type, it is returned as the wrong pointer.

C + + language variable problems (global variables, local variables, static global variables, static local variables)

Related Article

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.