Detailed resolution of the differences between global and local variables in memory

  • 2020-04-02 01:53:21
  • OfStack

One, preparatory knowledge - the program memory allocation

The memory used by a c/ c ++ compiled program is divided into the following sections

1. Stack - is automatically allocated by the compiler to release, stored function parameter values, local variable values, etc. It operates like a stack in a data structure.

2. Heap area Release is usually allocated by the programmer. If the programmer does not release, the program may end up being recycled by the OS. Note that it is not the same as the heap in a data structure, but is allocated in a way similar to a linked list.

3. Global area (static area) (static) --, global variables and static variables are stored together, the initialized global variables and static variables are stored in one section (.data), and the uninitialized global variables and uninitialized static variables are stored in another adjacent section (.bss). - the program is released by the system after completion.

4. Text constant area - the constant string is the (.rodata) that you put here. When the program is finished, it is released by the system.

5. Program code area -- the binary code (.text) that holds the body of the function.

Two, example procedures
This is written by a senior, very detailed


//main.cpp
int a = 0;          //Global initialization area
char *p1;           //Global uninitialized area
main()
{
  int b;            //The stack area
  char s[] = "abc"; //The stack area
  char *p2;         //The 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-byte regions allocated are in the heap

  strcpy(p1, "123456");    //"123456/0" is in the constant area, the compiler might put it
                              //With p3 pointed to "123456" optimized into a place.
} 

What's the difference between a static global variable and a normal global variable? What's the difference between a static local variable and a normal local variable? What's the difference between a static function and a normal function?

A:
1)
The description of a global variable (an external variable) is prefixed with static to constitute a static global variable. Global variables are themselves statically stored, and static global variables are of course statically stored. The two are no different in the way they are stored. The difference between the two is that the scope of the non-static global variables is the entire source program, and when a source program is composed of multiple source files, the non-static global variables are valid in each source file. A static global variable limits its scope, meaning that it is only valid in the source file where the variable is defined and cannot be used in other source files in 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, you can avoid causing errors in other source files.

2) From the above analysis, it can be seen that changing a local variable to a static variable changes its storage mode, that is, its lifetime. Changing a global variable to a static variable changes its scope and limits its use.                                

3) The static function is different from the normal function scope, only in this file. Functions that are used only in the current source file should be declared as internal (static), and internal functions should be declared and defined in the current source file. For functions that can be used outside the current source file, it should be stated in a header file that the source file to use the functions contains

In summary:

What's the difference between a static global variable and a normal global variable?

Static global variable is initialized only once to prevent it from being referenced in other file units;

What is the difference between static local variables and ordinary local variables?

Static local variables are initialized only once, the next time according to the previous result value;

What is the difference between static function and normal function?

The static function has only one copy in memory, and the normal function maintains one copy per call

  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
A practical example of C language variable allocation:
 
Let's take a look at where variables are allocated in an executable file. For example, an executable file has a fixed memory load address, and a symbol (the name of the function/variable) can be identified in advance as a future memory address connector.

The result of the source program compilation connection is a heap of assembly instruction code, roughly divided into. Text. Data. BSS and other sections. For. Exe and. So files, global and static variables are placed in. Data or. Type; Whether or not to initialize. The initialized variables are then assigned locations and Spaces in the.data section, uninitialized variables in the.bss section, and undefined variables in the.undef section. In assembly code, the global variable is represented as a memory address (the global variable is an offset value in the target file and a memory address when loaded into memory). The temporary variable becomes ebp/esp+n in assembly code, represented as a stack address, as part of the program body (.text). The final memory address of some variables cannot be determined before it is loaded into memory and can be calculated only after it is loaded into memory.

Global variable scopes span multiple source programs. Therefore, global variables cannot be renamed. The static variable scope is within a single source program. Multiple source programs can have global static variables of the same name. In this example, GCC USES c444 and c444.0 to distinguish between multiple static variables with the same name.


[test@redhat]// more aaa.c
# include <stdio.h>
int a111 = 0;              //The global variable is initialized
char *p111 = "654321";     //The global pointer variable has been initialized
static int c444 = 9;       //The static global variable has been initialized
static int c555;           //The static global variable is not initialized
main() 
{ 
    int b222;              //A local variable
    char s333[] = "abc";   //A local variable
    char *p222;            //A local variable
    char *p333 = "123456";    //A local variable
    static int c444 =0;       //The static local variable is initialized and has the same name as the previous static global variable
    p111 = (char *)malloc(10); 
    p222 = (char *)malloc(20);
    strcpy(p111, "123456");
}


Related articles: