The verification of global variables, local variables, heap and stack in C language program in Ubuntu

The verification of global variables, local variables, heap and stack in C language program in Ubuntu

Global variables and local variables

1. The difference between global variables and local variables is that the scope of the variable is different. Global variables are defined in the entire program and act on all processes, while most local variables are defined in a function and only act in this function. , Out of this range, it will no longer take effect.
2. Different memory storage methods: global variables are stored in the global data area, and local variables are stored in the stack area

Verify under Ubuntu

1. Verification procedure

#include <stdio.h>
int n = 10;  //全局 变量
void func1(){
    
    
    int n = 20;  //局部变量
    printf("func1 n: %d\n", n);
}
void func2(int n){
    
    
    printf("func2 n: %d\n", n);
}
void func3(){
    
    
    printf("func3 n: %d\n", n);
}
int main(){
    
    
    int n = 30;  //局部变量
    func1();
    func2(n);
    func3();
    //代码块由{}包围
    {
    
    
        int n = 40;  //局部变量
        printf("block n: %d\n", n);
    }
    printf("main n: %d\n", n);
    return 0;
}

2. Verification result
Insert picture description here
Analysis: From this, the scope of the two variables can be clearly seen.
1. For func1(), the output result is 20. Obviously, the n inside the function is used instead of the outside n; the same is true for func2(). When the global variable and the local variable have the same name, the local variable has priority in the local scope.
2. Func3() outputs 10, which is a global variable. Because there is no local variable n in the func3() function, the compiler can only find the variable n outside the function, that is, in the global scope.

Heap and stack

1. The stack area is automatically allocated and released by the compiler, storing function parameter values, local variable values, etc. Its
operation is similar to the stack in the data structure.
2. The heap is generally allocated and released by the programmer. If the programmer does not release it, it may be recovered by the OS when the program ends
. Note that it is different from the heap in the data structure, and the allocation method is similar to a linked list.
3. The global area (static area) (static), the storage of global variables and static variables are placed together, initialized
global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are adjacent Another
area of. Released by the system after the program ends.

Verify under Ubuntu

1. Verification procedure

#include <stdio.h>
#include <malloc.h>
int main(void)
{
    
    
    /*在栈上分配*/
    int  i1=0;
    int  i2=0;
    int  i3=0;
    int  i4=0;
    printf("栈:向下\n");
    printf("i1=0x%08x\n",&i1);
    printf("i2=0x%08x\n",&i2);
    printf("i3=0x%08x\n",&i3);
    printf("i4=0x%08x\n\n",&i4);
     printf("--------------------\n\n");
    /*在堆上分配*/
    char  *p1 = (char *)malloc(4);
    char  *p2 = (char *)malloc(4);
    char  *p3 = (char *)malloc(4);
    char  *p4 = (char *)malloc(4);
    printf("p1=0x%08x\n",p1);
    printf("p2=0x%08x\n",p2);
    printf("p3=0x%08x\n",p3);
    printf("p4=0x%08x\n",p4);
    printf("堆:向上\n\n");
    /*释放堆内存*/
    free(p1);
    p1=NULL;
    free(p2);
    p2=NULL;
    free(p3);
    p3=NULL;
    free(p4);
    p4=NULL;
    return 0;
}

2.
Insert picture description here
Analysis of verification results : It can be found that the stack area in the memory is mainly used to allocate local variable space. At a relatively high address, the stack address increases downward; while the heap area is mainly used to allocate the memory requested by the programmer Space, the heap address grows upward.

Guess you like

Origin blog.csdn.net/rude_dragon/article/details/110527927