First, the preparation of knowledge-Program memory allocation
The memory used by a program compiled by C + + is divided into the following sections
1, stack area (stack)-by the compiler automatically assigned to release, store the function of the parameter values, local variables and other values. The operation is similar to the stack in the data structure.
2, Heap area (heap) -generally by the programmer assigned to release, if the programmer does not release, the program at the end may be reclaimed by the OS. Note that it is different from the heap in the data structure, and the distribution is similar to the linked list.
3, Global zone (static area)-, the storage of global variables and static variables is placed in one piece, initialized global variables and static variables in a region (. data), uninitialized global variables and uninitialized static variables in another contiguous area (. BSS). -released by the system after the program is finished.
4, literal constant area -the constant string is here (. rodata). The system is released after the program is finished.
5, program code area -the binary code (. Text) that holds the function body.
Second, the example procedure
This is written by a predecessor, very detailed
Copy Code code as follows:
int a = 0; Global initialization Area
Char *p1; Global uninitialized Zone
int b; Stack area
Char s = "ABC"; Stack area
Char *p2; Stack area
Char *p3 = "123456"; "123456/0" in the constant area, p3 in the stack area
static int c = 0; Global (static) initialization area
P1 = (char *) malloc (10);
P2 = (char *) malloc (20); The 10 and 20 bytes allocated are in the heap area.
strcpy (P1, "123456"); "123456/0" is placed in the constant area, and the compiler might
And P3 point to "123456" to optimize into a place.
What is the difference between a static global variable and a normal global variable? What is the difference between a static local variable and a normal local variable? What is the difference between a static function and a normal function?
1 The global variable (external variable) before the description and then the static to form a global variable. The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are not different in the way they are stored. The difference between the two is that the scope of the Non-static global variable is the entire source program, and 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, that is, it is only valid within the source file that defines the variable, and it 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 only be common to functions within that source file, so it is possible to avoid causing errors in other source files.
2 from the above analysis can be seen, the local variable to the static variable is changed its storage mode that changes its lifetime. Changing the global variable to a static variable changes its scope and limits its use.
3 The static function differs from the normal function scope, only in this file. Functions that are used only in the current source file should be described as internal functions (static), and internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, you should indicate in a header file that the source file to use these functions contains this header file
What is the difference between a static global variable and a normal global variable:
The static global variable is only initialized once, preventing it from being referenced in other file cells;
What is the difference between a static local variable and a normal local variable:
The static local variable is initialized only once, and the next time based on the last result value;
What is the difference between a static function and a normal function:
The static function has only one copy in memory, and the normal function maintains a copy of each call
A practical example of a C-language variable assignment:
Let's take a look at where the variables are allocated in the executable file. Here is an example of an executable file that has a fixed memory-loading address, and a symbol (the name of a function/variable) in the future the address connector in memory can be determined in advance.
The result of the source program compilation connection is the formation of 1 heap assembly instruction code, which is roughly divided into several sections, such as. text. Data. BSS. For. exe files and. So files, both global and static variables are placed in the. data or. BSS segment (gas scans the source file from start to finish 1 times before it knows all about a variable: whether it is defined; The initialization variables are then allocated in the. Data section, where the positions and spaces are assigned, and the uninitialized variables are allocated in the. BSS segment, and the undefined variables are assigned to the. UNDEF segment. The global variable in the assembly instruction code is represented as a memory address (the global variable is an offset value in the destination file and is loaded into memory as a memory address). The temporary variable becomes ebp/esp+n in the assembly code, represented as a stack address, as part of the program body (. Text). The final memory address of some variables cannot be determined until it is loaded into memory, and it needs to be loaded into memory before it can be computed.
Global variable scopes are spanned by multiple source programs. Therefore, global variables cannot be duplicate names. A static variable scope is located within a single source program. Multiple source programs can have global static variables of the same name. In this case, GCC distinguishes between multiple static variables with the same name, using c444 and c444.0.
Copy Code code as follows:
# include <stdio.h>
int a111 = 0; Global variable already initialized
Char *p111 = "654321"; Global pointer variable already initialized
static int c444 = 9; Static global variable already initialized
static int c555; Static global variable not initialized
int b222; Local variables
Char s333 = "ABC"; Local variables
Char *p222; Local variables
Char *p333 = "123456"; Local variables
static int c444 = 0; Static local variable initialized with duplicate previous static global variable
p111 = (char *) malloc (10);
p222 = (char *) malloc (20);
strcpy (p111, "123456");