Home > Common Problem > What is the difference between static variables and global variables?

What is the difference between static variables and global variables?

silencement
Release: 2019-07-08 10:43:11
Original
8621 people have browsed it

What is the difference between static variables and global variables?

Global variables and static variables

static The declared variables have two characteristics in C language:

1. The variables are placed in the program in the global storage area, so that the original assignment can be maintained the next time it is called. This is the difference between it and stack variables and heap variables.

2. Variables use static to tell the compiler that they are only visible within the scope of the variable. This is what differentiates it from global variables.

Tips:

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

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

C. Design and use to access dynamic global variables , static global variables, static local variables, we need to consider the reentrancy issue;

D. If we need a reentrant function, then we must avoid using static variables in the function (such The function is called: a function with "internal memory" function)

E. When static variables must be used in the function: For example, when the return value of a function is a pointer type, it must be static The address of the local variable is used as the return value. If it is of auto type, an error pointer is returned.

Adding static before a function makes the function a static function. But the meaning of "static" here does not refer to the storage method, but refers to the scope of the function being limited to this file (so it is also called an internal function). The advantage of using internal functions is that when different people write different functions, they don't have to worry about whether the functions they define have the same name as functions in other files.

Extended analysis: The term static has an unusual history. Initially, the keyword static was introduced in C to indicate local variables that still exist after exiting a block. Subsequently, static has a second meaning in C: used to represent global variables and functions that cannot be accessed by other files. Finally, C reuses this keyword and gives it a third meaning that is different from the previous one: denoting variables and functions that belong to a class rather than to any specific object belonging to that class (the same meaning as this keyword in Java).

The difference 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.

According to storage area, global variables, static global variables and static local variables are all stored in the static storage area of ​​​​the memory, and local variables are stored in the stack area of ​​​​the memory.

According to scope, global variables are valid within the entire project file; static global variables are only valid within the file in which they are defined; static local variables are only valid within the function in which they are defined, and are only allocated once by the program. Memory, the variable will not disappear after the function returns; local variables are valid within the function in which they are defined, but become invalid after the function returns.

The description of global variables (external variables) is preceded by static to form a static global variable. Global variables themselves are static storage methods, and static global variables are of course also static storage methods. There is no difference between the two in the way they are stored. The difference between the two is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are valid in each source file. Static global variables limit their scope, that is, they are only valid within the source file in which the variable is defined, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be shared by functions in that source file, errors can be avoided in other source files.

It can be seen from the above analysis that changing a local variable to a static variable changes its storage method and changes its lifetime. Changing a global variable to a static variable changes its scope and limits its scope of use.

The static function is different from the ordinary function scope and is only valid within the source file in which the variable is defined. Functions used only in the current source file should be declared as internal functions (static), and internal functions should be described and defined in the current source file. Functions that can be used outside the current source file should be stated in a header file, and the source files that use these functions should include this header file.

What is the difference between static global variables and ordinary global variables: static global variables are only initialized once to prevent them from being referenced in other file units;

static local variables are different from ordinary local variables. What is the difference: static local variables are only initialized once, and the next time is based on the previous result value;

What is the difference between static functions and ordinary functions: static functions have different scopes from ordinary functions, only the source of the variable is defined Valid within the file;

If global variables and static variables are not manually initialized, they will be initialized to 0 by the compiler. The values ​​of local variables are not known.

The above is the detailed content of What is the difference between static variables and global variables?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template