C++ program memory allocation at runtime

When a c++ program is running, the following memory areas are planned

1. Stack, which stores function parameter values, local variable values, etc., is automatically allocated and released by the system. First in last out, the memory is continuous.

2. The heap heap is allocated and released by the programmer. If it is frequently applied but not released, there will be a memory leak problem, and it may be recycled by the OS when the program ends. The heap memory is not continuous, it is more like a linked list in the data structure.

3. The global static area stores global variables and static variables , among which the initialized ones are put together, and the non-initialized ones are put together. Released by the os after the program ends

4. The constant area stores constants, including character constants. Released by the os after the program ends.

5. The program code area stores the binary code of the function body.

Brief description of the stack

The stack is a continuous block of memory. If it is not large, an overflow error will appear if it exceeds it. It is addressed from a high address to a low address. Since stack memory is allocated continuously, reading is faster.

When a function is called, the next instruction address of this function is first pushed onto the stack. This is because the stack is first-in, last-out. After calling the function, you must know what the next call address is, and let the program continue to execute.

Then the parameters of the function are pushed onto the stack from right to left

Then the local variables in the function body are pushed onto the stack, and the rest of the static variables are not pushed onto the stack.

It is occupied when it is pushed into the stack, and it is released when it is popped out, and they are all pushed into the stack in sequence, and then popped out in reverse order, so the stack occupancy is guaranteed to be continuous, so it is very fast to find available stack memory.

Brief description of the heap

The heap is very large, and the occupancy is discontinuous. When you create a new object, you allocate memory and query a linked list in the os. This linked list records information about the available heap memory. Each query will traverse the linked list until it finds the first memory block that can hold the object, write the occupied size into the first address, and write the rest into the object. Changes such as endless use and occupation will update the information in the linked list. So heap allocated memory is slower than stack.

If you run out of this object, you should release it but not, then the memory corresponding to this object is still occupied. If there are too many such objects, a lot of memory will be occupied, causing a memory leak.

If a pointer is defined but not initialized, then the pointer may point to any memory address, and it is very dangerous to use this pointer at this time.

It is also very dangerous if a pointer memory address is reclaimed, but the pointer is not used and set to null.

Guess you like

Origin blog.csdn.net/opk8848/article/details/127514781