C language foundation 4 ~ memory 2

Summary post for my study -c memory 2~~ whaosoft  aiot  http://143ai.com

1. C/C++ memory management

 C language memory management refers to the allocation, creation, and use of system memory.

    In a computer, the memory between each application is independent of each other. Usually, application A cannot access application B. Of course, some special techniques can access it, but this article does not explain it in detail. For example, in a computer, the memory of a video player program and a browser program cannot be accessed, and the memory owned by each program is managed in partitions.

    In the computer system, running program A will open up program A's memory area 1 in the memory, and running program B will open up program B's memory area 2 in the memory, and memory area 1 and memory area 2 are logically separated.

 The memory area 1 opened up in program A will be divided into several areas, which are the four areas of memory, and the four areas of memory are divided into stack area, heap area, data area and code area.

   The stack area refers to the area where some temporary variables are stored. Temporary variables include local variables, return values, parameters, return addresses, etc. When these variables exceed the current scope, they will be automatically popped up. The maximum storage of the stack has a size, and this value is fixed, and exceeding this size will cause stack overflow.

    The heap area refers to a relatively large memory space, which is mainly used for the allocation of dynamic memory; in program development, it is generally the developer who allocates and releases it. If it is not released at the end of the program, the system will automatically recycle it.

    The data area refers to the area that mainly stores global variables, constants and static variables, and the data area can be divided into global area and static area. Global variables and static variables will be stored in this area.

    The code area is easier to understand. It mainly stores executable code, and the attributes of this area are read-only.

Use code to confirm the underlying structure of the four memory areas

    Since the underlying structures of the stack area and the heap area are relatively intuitive, the code used here only demonstrates these two concepts. First look at the code to observe the memory address allocation in the stack area:

  The result of the operation is: 

 

     We can observe that the address of variable a is 2293324 and the address of variable b is 2293320. Since the data size of int is 4, the interval between the two is 4; then look at variable c, we find that the address of variable c is 2293319, which is the same as that of variable b The address of 2293324 is spaced by 1 because the data type of c is char and the type size is 1. Here we observe that when I create variables, the order is from a to b and then to c, why the address between them is not increased but decreased? That's because a data storage structure in the stack area is first-in, last-out, as shown in the figure:

    First of all, the top of the stack is the "minimum" index of the address, and then it increases sequentially downwards, but due to the special storage structure of the stack, we store the variable a first, then one of its index addresses will be the largest, and then decrease in turn ;The value stored for the second time is b, whose address index is smaller than that of a. Since the data size of int is 4, the address of a is reduced by 4 to 2293320. When storing c, it is char , the size is 1, the address is 2293319. Since the three variables a, b, and c belong to the same stack, the indexes of their addresses are continuous. 

Guess you like

Origin blog.csdn.net/qq_29788741/article/details/131941847