Virtual memory in C/C++


1. Virtual memory

Virtual memory is a memory management technology implemented between computer software and hardware. It maps the memory address (virtual address) used by the program to the physical address in the computer memory. Virtual memory makes the application program free from cumbersome memory space management. Liberated, improved memory safety through memory isolation.

The virtual memory address is usually a continuous address space, which is controlled by the memory management module of the operating system. When a page fault interrupt is triggered, the actual physical memory is allocated to the virtual memory by using paging technology. At the same time, the space size of the virtual memory far exceeds the size of the actual physical memory. The virtual memory technology enables the process to use a much larger memory space than the physical memory.


2. The virtual memory allocation model in C

insert image description here

The memory image of a process in C language is divided into five parts: the text segment, the initialized data segment, the uninitialized data segment, the heap area, and the stack area from the low address:

  1. Text segment : Text segment is used to storeprogram execution codeA memory area, which is the image of the binary file (or the processor's machine instructions) in memory . Of course, it is also possible to contain some read-only constant variables, such as string constants, etc. The size of this part of the area has been determined before the program runs, and usually only read operations are allowed, and writing to the Text segment will cause a Segmention Fault.
  2. Data segment : The Data segment refers to the storage programInitialized global and static variablesa memory area. The Data segment is not anonymous, but maps the data in the program binary file that has been initialized at compile time, and is initialized by the program .
  3. BSS segment : BSS segment is storedUninitialized global and static variables, initialized (cleared) by the operating system , and is anonymous, does not map any files, does not occupy external memory space, and only occupies memory during runtime. Note that uninitialized here refers to no explicit initialization, because global variables and static variables will be automatically and implicitly initialized to 0, but we do not need to store these 0s to save external memory space, which is also the main purpose of the BSS segment effect.
  4. Heap area : the heap providesMemory allocation while the program is running, the life cycle of the heap memory is outside the function. Most languages ​​provide heap memory management functions, such as and in C language malloc(). free()Therefore, the heap area is managed by the user and is highly controllable. If the current heap memory is enough for the program, there is no need to interact with the kernel, just find the available memory in the current heap, otherwise, you need to call the brk()system call to apply for space from the kernel. The algorithm of heap memory allocation is very complicated, and it is necessary to ensure the memory allocation Real-time and fast, and try to avoid too much fragmentation in the heap, which leads to a series of memory allocation algorithms and allocation strategies in the operating system, such as FF, BF, WF, NF, etc.
  5. Stack area : the stack is savedLocal variables, function parameters, return valuesetc. Calling a new function creates a new stack frame on the stack, which is automatically destroyed when the function returns. A stack frame includes: the return address and parameters of the function, temporary variables (including non-static local variables of the function and other temporary variables automatically generated by the compiler), stack frame state values ​​(EBP and ESP, which define the stack frame of this function range). The space allocation of the stack is slightly different from that of the heap. When the stack space is exhausted, continuing to push will trigger the expansion of the stack space, resulting in Page Fault, and then called in the kernel. This function is called to determine whether the stack space can be increased. If the current expand_stack()stack acct_stack_growth()space If the size is smaller than RLIMIT_STACK, the stack space can continue to grow, and the process will not be perceived by the kernel to complete the process. When the user's stack space has reached the maximum value allowed, the kernel will send a Segmentation Fault signal to the process to terminate the process, so the stack space of the process will only increase and not shrink.

3. Virtual memory allocation model in C++

Virtual memory allocation in C++ is largely similar to that in C, with slightly different details:

  1. Constant area : The constant area is a relatively special storage area, which is specially used to store variables modified by const and constant strings that cannot be modified , and will be released by the system after the end of the program.
  2. Global/static storage area : store global variables and static variables, and this area will exist once the program is compiled. In C++, since the compiler automatically initializes and assigns global variables and static variables, it does not distinguish between initialized variables and uninitialized variables like in C. The global/static memory area will be freed by the system after the program ends.
  3. Free storage area : The free storage area is memory allocated by malloc/calloc/realloc and needs to be released by free . If the programmer does not perform the free operation, it will cause a memory leak, and the memory will be reclaimed by the OS at the end of the program.
  4. Heap area : Unlike in C, the heap in C++ is memory allocated by new and released by delete or delete[] .
  5. Stack area : save the local variables, function parameters and return value in the function. The compiler is responsible for allocating and releasing, and the stack variables will also become invalid at the end of the function.

Fourth, the difference between the heap area and the stack area

heap area stack area
manager Managed by users, strong controllability Managed by the system, high distribution efficiency
address extension Extend from low address to high address Extend from high address to low address
System response after application If the memory of the current heap is enough for the program, there is no need to interact with the kernel, just continue to search for available memory in the current heap. Otherwise, you need to call brk()the system call to apply for space from the kernel expand_stack()When the stack space is exhausted, continuing to request will trigger the expansion of the stack space, resulting in a page fault, and then call it in the kernel acct_stack_growth()to determine whether the stack space can be increased. If the current stack space size is less than RLIMIT_STACK, the stack can continue to grow space. When the user's stack space has reached the maximum value allowed, the kernel will send a segment fault signal to the process to terminate the process.
storage content Memory allocation at runtime is up to the user The stack stores local variables, function parameters, return values, etc.
life cycle The life cycle of the heap memory is outside the function and controlled by the user Calling a new function creates a new stack frame on the stack, which is automatically destroyed when the function returns

Guess you like

Origin blog.csdn.net/qq_43686863/article/details/129880279