Memory learning (1) memory allocation in four areas

1. The memory composition of the C language program

For a C language program, the memory occupied mainly includes the following parts: code area (written C language code and data), static constant (character constant) area (defined string constant), initialized global Data area (global variables are initialized), uninitialized global data area, heap area, and stack area. The
stack area grows in the direction of decreasing address, and the heap area grows in the direction of increasing address.
The main function and self-defined functions are temporary, so in Open up space on the stack, and the size of the open space is determined by the type of the variable. The space opened up is the stack frame
. Assuming that the address bus is 32 bits, the addressable range is 4G, and the memory address is 0 - 0xffffffff
insert image description here

2. Divide memory into four areas

The memory is divided into 4 areas:

  1. Code area: store the binary code of the function body, managed by the operating system
  2. Global area: store global variables and static variables and constants
  3. Stack area: automatically allocated and released by the compiler, storing function parameter values, local variables, etc.
  4. Heap area: allocated and released by the programmer, if the programmer does not release it, it will be reclaimed by the operating system after the program ends
memory partition name content permissions
stack area Ordinary variables in functions readable and writable
heap area Dynamically allocated memory readable and writable
static variable area static modified variables readable and writable
data area Constants for initializing variables read only
code area code instruction read only

2.1 Code area

  1. The code area stores the machine instructions executed by the CPU, which is the binary code compiled by the program
  2. The code area is shared. For frequently executed programs, only one copy needs to be saved in the memory.
  3. The code area is read-only. The reason for reading-only is to prevent the program from accidentally modifying the instructions.

2.2 Global zone

insert image description here

2.3 Stack area

It is automatically allocated and released by the compiler to store function parameter values, local variables, etc.
Note: Do not return the address of the local variable, the address opened by the stack area is automatically released by the compiler

2.4 Heap area

It is allocated and released by the programmer. If the programmer does not release it, the operating system will recycle it at the end of the program. The malloc keyword is mainly used to open up the heap area

3. The difference between heap and stack

3.1 How to apply

Stack area stack : automatically allocated by the system and assigned at runtime. As long as the remaining space of the stack is greater than the requested space, the system will provide memory for the program, otherwise an exception will be reported to prompt stack overflow. For example, if a local variable is declared in a function, the system
automaticallyint a; Open up space for a in the stack

Heap area heap : The programmer needs to apply for it himself, and specify the size, which is determined at compile time. When using the malloc function in C language, the free statement must be used to release the memory space correctly. For example
:char * p1; p1 = (char *)malloc(10);

3.2 The address size of the application

Stack : The stack is a data structure that expands to low addresses. It is a continuous memory area that is automatically allocated by the system. The speed is faster, but the programmer cannot control it.

Heap : The heap is a data structure that expands to high addresses. It is a discontinuous memory area. It is relatively slow and prone to memory fragmentation.

3.3 Storage content in the heap and stack

Stack : When a function is called, the first thing to be pushed onto the stack is the address of the next instruction in the main function, and then the parameters of the function. The parameters are pushed into the stack from right to left, and then the local variables in the function. Static variables are not pushed onto the stack. When this function call ends, the local variables are popped out of the stack first, then the parameters, and finally the pointer on the top of the stack points to the address where it was originally stored, and the program continues to run from this point

Heap : Generally, one byte is used to store the size of the heap at the head of the heap, and the specific content is arranged by the programmer

Guess you like

Origin blog.csdn.net/future_sky_word/article/details/125704903