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

Source: Internet
Author: User

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

C ++ variables have different scopes based on different lifecycles at defined locations. They can be divided into six categories: global scope, local scope, statement scope, and Class scope, namespace scope and file scope.

From the scope:

Global variables have a global scope. Global variables can be defined in only one source file to act on all source files. Of course, other source files that do not contain the definition of a global variable must use the extern keyword to declare the global variable again.

The static local variable has a local scope and is initialized only once, since it is initialized for the first timeProgramThe Operation has always ended. The difference between global variables and global variables is that global variables are visible to all functions, while static local variables are only visible to the defined function bodies.

A local variable only has a local scope. It is an automatic object (auto). It does not always exist during the program running, but only exists during the function execution. After a function call is completed, the variable is revoked, and the occupied memory is also withdrawn.

Static global variables also have a global scope. The difference between static global variables and global variables is that if a program contains multiple files, it acts on the files that define it and cannot act on other files, the variable modified by the static keyword has the file scope. In this way, even if two different source files define static global variables with the same name, they are also different variables.

From the perspective of memory space allocation:
Global variables, static local variables, and static global variables are all allocated space in the static storage area, while local variables are allocated space in the stack.

 

 

Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

1) static variables will be placed in the static data storage area (data segment) (globally visible) of the program, so that the original values can be assigned during the next call. This is the difference between stack variables and heap variables.
2) The variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.

From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use. Therefore, the description of static plays different roles in different places. Attention should be paid.

TIPS:
A. if the global variable is only accessed in a single c file, you can change the variable to a static global variable to reduce the coupling between modules;
B. If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;
C. When designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider re-import, because they are all stored in the static data storage area, which is globally visible;
D. If we need a reentrant function, we must avoid using static variables in the function (such a function is called a function with the "internal memory" function)
E. static variables must be used in a function. For example, if the return value of a function is of the pointer type, the address of the static local variable must be used as the return value. If the return value is of the auto type, the returned result is an error pointer.

Bytes -----------------------------------------------------------------------------------------------------------

Static global variable: change the scope of action without changing the storage location

Static local variable: changes the storage location and does not change the scope.

Static functions: add the static keyword before the return type of the function. The function is defined as a static function. A static function is different from a common function. It can only be seen in the file where it is declared and cannot be used by other files.

A function defined in a source file can only be called by functions in this file, but cannot be called by functions in other files of the same program. This function is also called an internal function. To define an internal function, you only need to add a "static" keyword before the function type.

Bytes ---------------------------------------------------------------------------------------------------------------

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.