The program is running in memory structures

Contact programming people know that high-level language can access the data in memory variable names .
1. So those variables in memory is how to store it?
2. The program is how to use these variables it?

First, look at the variable C language how they are distributed in memory.
C language has a global variable (Global), local variables (Local), static variables (Static), register variables (Regeister) . Each variable has a different distribution. First look at the following code:

#include <stdio.h>
int g1=0, g2=0, g3=0; //函数外的全局变量
int main()
{
    static int s1=0, s2=0, s3=0;
    int v1=0, v2=0, v3=0;
    //打印出各个变量的内存地址    
    printf("0x%08x\n",&v1); //打印各本地变量(函数内)的内存地址
    printf("0x%08x\n",&v2);
    printf("0x%08x\n\n",&v3);
    printf("0x%08x\n",&g1); //打印各全局变量(函数外)的内存地址
    printf("0x%08x\n",&g2);
    printf("0x%08x\n\n",&g3);
    printf("0x%08x\n",&s1); //打印各静态变量(static修饰)的内存地址
    printf("0x%08x\n",&s2);
    printf("0x%08x\n\n",&s3);
    return 0;
}

The results are compiled:

0x0012ff78 //打印各本地变量(函数内)的内存地址
0x0012ff7c 
0x0012ff80

0x004068d0 //打印各全局变量(函数外)的内存地址
0x004068d4
0x004068d8

0x004068dc //打印各静态变量(static修饰)的内存地址
0x004068e0
0x004068e4

The result is that the memory address of the variable output. Wherein v1, v2, v3 is a local variable, g1, g2, g3 are global variables, s1, s2, s3 is a static variable.
can be seen:

  1. The same type of variables in memory is a continuous distribution
  2. Local and global variables allocated memory address quite different.
  3. Global and static variables allocated memory is continuous.

This is because the local and global variables / static variables is the result of different types of memory allocated region. For a process in terms of memory space, it may be divided into three portions logically: area code, static data and dynamic data region area .
Code area: Binary code stored function body.
Dynamic data area: General is the "stack." "Stack (Stack)" and "heap (heap)" are two different dynamic data area, a linear structure stack, the stack is a chain structure . Stack may be described by a "base address" and "stack" address.

  1. Stack District : automatic variables. During a function, the function of the storage unit can create a local variable on the stack, the memory cells by the compiler automatically released at the end of the function execution . Stack for the function parameters, local variables, and the like.
  2. Heap (free store): When the caller is running (such as new C of malloc or in C ++) to allocate memory, you can determine the size of memory allocation and distribution at any time, the user himself is responsible for freeing memory when ( as used free or delete). Everything heap is anonymous, so that can not be accessed by name, but only accessed through a pointer .

Static data area: Existence was only allocated when the program starts, and may be executed until the beginning of the program was only initialization, as a function of static variables is the first to execute custom code when the variable is initialized in the program. The allocated memory are present during the entire operation of the program, such as * global variables and static variables are allocated in the static data area, the local dynamic data variables are allocated in the area, i.e. the stack. ** for accessing local variables base address and an offset stack. ** Note: ** initialized global and static variables in an area, uninitialized global variables and static variables in an area adjacent to another, while uninitialized object store can be accessed and manipulated through a void *, after the end of the program released by the system itself.

├———————┤		低端内存区域
│ …… │
├———————┤
│ 动态数据区 │	**堆栈区域**
├———————┤
│ …… │
├———————┤
│ 代码区 │
├———————┤
│ 静态数据区 │
├———————┤
│ …… │
├———————┤		高端内存区域

[Reference documentation] (https://blog.csdn.net/myqq1418/article/details/81584761)

Released two original articles · won praise 0 · Views 111

Guess you like

Origin blog.csdn.net/ky_zhang/article/details/104668015