1. Differences between local variables and global variables
A local variable can have the same name as a global variable, but a local variable masks a global variable . To use global variables, you need to use::. Referencing variables in a function body uses local variables of the same name instead of global variables, and for some compilers, multiple local variables with the same name can be defined within the same function body. For example, we can define a local variable I with the same name in two loops within a function, and the scope of the local variable i is within that loop body
Specifically, the difference between a global variable and a local variable is as follows:
1. Scopes are different: Global variables are scoped to the entire program, while local variables are scoped to the current function or loop, etc.
2. Memory storage in different ways: global variables stored in the global data area, local variables stored in the stack area
3. Life time is different: the life of the global variable is the same as the main program, destroyed with the destruction of the program, local variables inside the function or inside the loop, with the function exit or circular exit does not exist
4. Use different: Global variables can be used in various parts of the program after declaration, but local variables can only be used locally. local variables and global variables are preferred within the function
It is important to note that a local variable cannot be assigned the value of a global variable with the same name.
The Advantage of using global variables is that you can reduce the number of variables and reduce the time consumed by data passing by actual parameters and formal parameters. However, there are a number of drawbacks to using global variables: (1) Global variables are stored in a static storage area, memory is allocated when the program starts running, and the program ends freeing the memory. Compared with the dynamic allocation and dynamic release of local variables, the lifetime is relatively long, so too many global variables will occupy more memory units. (2) The global variable destroys the encapsulation performance of the function. Previous chapters have said that functions like a black box, generally through the function parameters and return values for input and output, the function of the internal implementation is relatively independent. However, if a global variable is used in a function, the statement in the function body can be accessed by bypassing the function parameter and the return value, which destroys the function's independence and causes the function to rely on the global variable. At the same time, the portability of the function is reduced. (3) Global variables make the code readability of the function less readable. Because multiple functions may use global variables, the values of global variables may change at any time when the function is executed, which is detrimental to the troubleshooting and debugging of the program. Therefore, if it is not a last resort, it is best not to use global variables.
2.
.......
....
.....
Conceptual Knowledge Summary of Linux