Discuss the location of local variables and global variables in memory in C language

  • 2020-04-02 00:45:58
  • OfStack

Local and global variables in the C language variable storage categories (static, extern, auto, register)

1. Local variables and global variables
As mentioned in the discussion of a function's argument, the argument allocates the memory unit only during the call and is released immediately after the call. This shows that the parameter is valid only within the function and cannot be used without the function. The scope of the validity of such a variable is called the scope of the variable. Not only for shapers, all quantities in C have their own scope. Variables are described in different ways and have different scopes. Variables in C language can be divided into two types according to scope scope, that is, local variables and global variables.
1.1 local variables
Local variables are also called internal variables. Local variables are defined within functions. Its scope is limited to a function, and it is illegal to use such a variable after leaving the function.
[example] 1.1

    int f1(int a)        
    {
        int b,c;     
    }
a,b,c effective 
    int f2(int x)        
    {
          int y,z;
   }
x,y,z effective 
    int main(void)
    {
         int m,n;
    }
m,n effective 

In function f1, three variables are defined. A is the parameter, b and c are the general variables. A,b,c are valid in the scope of f1, or the scope of a,b,c variables is limited to f1. Similarly, the scope of x,y, and z is limited to f2. The scope of m and n is limited to the main function. There's also scope for local variables The following points are explained:
1) variables defined in the main function can only be used in the main function, and cannot be used in other functions. Also, variables defined in other functions cannot be used in the main function. Since the main function is also a function, it is parallel to other functions. This is different from other languages and should be noted.
2) the parameter is a local variable belonging to the regulated function, while the real parameter is a local variable belonging to the main regulated function.
3) it is allowed to use the same variable name in different functions, they represent different objects, assign different units, do not interfere with each other, and do not cause confusion. As in the previous example, it is perfectly permissible for both parameter and argument to have variable names of n.
4) variables can also be defined in the compound statement, and their scope is only within the compound statement scope.
[example] 1.2

int main(void)
{
       int s,a;
      {
              int b;
              s=a+b;
             
       }

}

[example] 1.3

int main(void)
{
    int i=2,j=3,k;
    k=i+j;
    {
       int k=8;
       printf("%dn",k);
    }
    printf("%dn",k);
}

This procedure defines I,j,k three variables in main, where k is not assigned an initial value. In the compound statement, a variable k is defined and assigned an initial value of 8. It should be noted that these two k's are not the same variable. K is defined by main outside the compound statement, and inside the compound statement by k defined in the compound statement. So line 4, k, is defined by main, with a value of 5. Line 7 outputs the value of k, which is in the compound statement and is acted upon by the k defined in the compound statement. Its initial value is 8, so the output value is 8. Line 9 outputs the values of I and k. I is valid throughout the program. Line 7 assigns 3 to I, so the output is also 3. While line 9 is outside the compound statement, the output k should be the k defined by main. This k value has been obtained as 5 from line 4, so the output is also 5.

1.2 global variables
Global variables, also known as external variables, are variables defined outside a function. It doesn't belong to any function, it belongs to a source file. Its scope is the entire source program. The use of global variables in functions should generally be described as global variables. Only global variables that have been explained within a function can be used. The descriptor for a global variable is extern. However, the use of global variables defined before a function within that function is not explained.
[example] 1.4

   int a,b;          
    void f1()         
    {
       ... 
    }
   float x,y;         
    int fz()          
    {
       ... 
    }
    Int main(void)           
    {
       ... 
    }

From the above example, we can see that a, b, x and y are all external variables defined outside the function and are all global variables. But x and y are defined after f1, and there's no statement of x and y in f1, so they're not valid in f1. A,b are defined at the beginning of the source program, so they can be used without instructions in f1,f2 and main.
Input the length, width and height l,w,h of the cube. Find the volume and the area of the three faces x times y,x times z, and y times z.

int s1,s2,s3;
int vs( int a,int b,int c)
{
    int v;
    v=a*b*c;
    s1=a*b;
    s2=b*c;
    s3=a*c;
    return v;
}
int main(void)
{
    int v,l,w,h;
    printf("ninput length,width and heightn");
    scanf("%d%d%d",&l,&w,&h);
    v=vs(l,w,h);
    printf("nv=%d,s1=%d,s2=%d,s3=%dn",v,s1,s2,s3);
}

An external variable has the same name as a local variable.

int a=3,b=5;     
max(int a,int b) 
{
    int c;
    c=a>b?a:b;
    return(c);
}
 int main(void)  
{
    int a=8;
    printf("%dn",max(a,b));
}

If the external variable has the same name as the local variable in the same source file, the external variable is "masked" within the scope of the local variable, that is, it does not work.

2. Storage category of variables
2.1 dynamic storage mode and static dynamic storage mode
We have already shown that variables can be divided into global variables and local variables from the perspective of their scope (that is, from the perspective of space).
From another point of view, from the point of view of the time (i.e. lifetime) when the value of a variable exists, it can be divided into static storage mode and dynamic storage mode.
Static storage: a method of allocating a fixed amount of storage space while a program is running.
Dynamic storage: a way to dynamically allocate storage space as needed during a program's run.
User storage space can be divided into three parts:
1) program area;
2) static storage area;
3) dynamic storage area;
Global variables are all stored in a static storage area, which is allocated to the global variables at the beginning of the program execution, and released when the program is finished. They occupy fixed storage locations during program execution, rather than dynamically allocating and releasing them.
The dynamic storage area stores the following data:
1) function form parameters;
2) automatic variable (local variable without static declaration);
3) real site protection and return address of function call;
For the above data, the dynamic storage space is allocated at the beginning of the function call and freed at the end of the function.
In c, each variable and function has two properties: the data type and the storage category of the data.

2.2 auto variable
Local variables in the function, if not declared as static storage category, are dynamically allocated storage space, the data is stored in the dynamic storage area. The parameters in the function and the variables defined in the function (including those defined in the compound statement) fall into this category and are allocated storage space when the function is called, which is automatically released at the end of the function call. Such local variables are called automatic variables. Automatic variables use the keyword auto to declare the storage category.
[example] 1.7

int f(int a)         
{
    auto int b,c=3;     
}

A is a parameter, b is an automatic variable, and c is given an initial value of 3. After executing the f function, the memory cells occupied by a, b and c are automatically released.
The keyword auto can be omitted, and if auto is not written, it is implicitly defined as "automatic storage category", which belongs to dynamic storage mode.

2.3 declare local variables as static
Sometimes, if you want the value of a local variable in a function to remain unchanged after the function call, you should specify the local variable as "static local variable" and declare it with the keyword static.
Examine the value of a static local variable.

f(int a)
{
    auto b=0;
    static c=3;
    b=b+1;
    c=c+1;
    return(a+b+c);
}
int main(void)
{
    int a=2,i;
    for(i=0;i<3;i++)
         printf("%d",f(a));
}

Description of static local variables:
1) static local variables belong to the category of static storage, and storage cells are allocated in the static storage area. Does not release during the entire run of the program. The automatic variable (dynamic local variable) belongs to the category of dynamic storage, occupies the dynamic storage space, and is released after the function call.
2) the initial value of static local variables is assigned at compile time, that is, the initial value is assigned only once; The initial value of an automatic variable is assigned at the time of a function call.
3) if no initial value is assigned when defining a local variable, the initial value of 0 (for numeric variables) or null (for character variables) is automatically assigned at compile time for static local variables. For an automatic variable, the value is an uncertain value if the initial value is not assigned.
Print the factorial values of 1 through 5.

int fac(int n)
{
    static int f=1;
    f=f*n;
    return(f);
}
int main(void)
{
    int i;
    for(i=1;i<=5;i++)
        printf("%d!=%dn",i,fac(i));
}

2.4 the register variables
To improve efficiency, C allows the value of a local variable to be placed in a register in the CPU. This variable is called a register variable and is declared with the keyword register.
Register variables are used.

int fac(int n)
{
    register int i,f=1;
    for(i=1;i<=n;i++)
        f=f*I;
     return(f);
}
int main(void)
{
    int i;
    for(i=0;i<=5;i++)
        printf("%d!=%dn",i,fac(i));
}

Description:
1) only local automatic variables and formal parameters can be used as register variables;
2) the number of registers in a computer system is limited, so it is impossible to define any number of register variables;
3) local static variables cannot be defined as register variables.

2.5 declare external variables with extern
External variables (that is, global variables) are defined outside a function and are scoped from the variable definition to the end of the program file. If an external variable is not defined at the beginning of the file, its valid scope is limited to where the definition ends at the end of the file. If the function before the point of definition wants to reference the external variable, you should use the keyword extern to "declare an external variable" before the reference. Indicates that the variable is an external variable that has been defined. With this declaration, you can use the external variable legally from the declaration point on.
Declare external variables with extern to extend the scope in the program file.

int max(int x,int y)
{
    int z;
    z=x>y?x:y;
    return(z);
}
int main(void)
{
    extern A,B;
    printf("%dn",max(A,B));
}
int A=13,B=-8;

Description: In the last line of this program file, the external variables A and B are defined. However, since the location of the external variables is defined after the function main, the external variables A and B cannot be referred to in the main function. Now we use extern in the main function to "declare an external variable" for A and B, so we can use the external variables A and B legally from the "declare" point.

Related articles: