In C + +, it is often difficult to tell the difference between global variables, local variables, and static local variables. This blog distinguishes these three variables from the variable storage attribute and the scope of the identifier at two angles.
First, let's look at how the memory area of a C + + program is allocated.
From that, we can see that the memory area of C + + program is divided into code area, global data area, heap area and stack area . Where global variables and static local variables are stored in the global data area, these two variables have been allocated and initialized at the beginning of the program. Ordinary local variables are stored in the stack, which is generated when the program enters the declared code block and is deleted when the code block is closed.
Secondly, from the perspective of the scope of identifiers, we can assume that:
1. Global variables and static local variables have a file scope ;
2. Ordinary local variables have function scope or block scope .
The difference between global variables, ordinary local variables, and static local variables in C + +