Virtual address space diagram

The allocation of virtual address space is related to the system environment: the
following picture shows the linux X86 environment

Virtual address
Insert picture description here

* 1. Reserved area (protected address)

The reserved area is the protected address, with a size of 0~4K, located in the lowest part of the virtual address space, and no physical address is assigned (it will not correspond to the memory address, so it will not put any content). Any reference to it is illegal and is used to catch the exception of using null pointers and pointers of small integer values ​​to refer to memory. In most operating systems, very small addresses are usually not allowed to be accessed, such as NULL.

The C language assigns invalid pointers to 0 for this reason, because under normal circumstances, valid and accessible data will not be stored at the 0 address. Assigning a pointer to 0 means that the pointer will never be used,
so there will be no wild pointers.
#define NULL 0 and #define NULL (void
)0 are equivalent in C language, but in C++
, you can only use #define NULL 0, and later use of #define NULL (void
)0 will make mistakes.
**

2. Code snippet

Code segment is also called body segment or text segment, and is usually used to store program execution code (that is, machine instructions executed by the CPU). Generally, C language execution statements are compiled into machine code and stored in the code segment. Usually code segments are sharable, so frequently executed programs only need to have a copy in memory. The code segment is usually read-only to prevent other programs from accidentally modifying its instructions (writing to this segment will cause a segmentation fault). Some architectures also allow the code segment to be writable, which means that the program can be modified.

3. Data section (.data section)

The data segment is usually used to store initialized global variables and static local variables in the program. The data segment belongs to static memory allocation (static storage area), readable and writable. Since the default value of global variables is 0 when they are not initialized, global variables with a value of 0 are located in the .bbs section (not in the data section). For uninitialized local variables, their value is unpredictable. Note: There are other sections between the code section and the data section: read-only data section and symbol section.

4... bbs segment

This section is used to store uninitialized global variables and static local variables, including global variables with a value of 0. The data segment and the .bbs segment are also called the global data area, the former is initialized, and the latter is not initialized.
ELF section includes: code section, other sections (read-only data section and symbol section, etc.), .data section (data section) and .bbs section, all of which belong to the executable program part.

5. Heap space

The space allocated by the new() and malloc() functions belongs to the pair space, which is used for the allocation of memory space, from bottom to top. The heap is used to store the memory segment dynamically allocated while the process is running, and can be dynamically expanded or reduced. The contents of the heap are anonymous and cannot be accessed directly by name, but can only be accessed indirectly through pointers. When the process calls malloc© and new (C++) functions to allocate memory, the newly allocated memory is dynamically added to the heap (expansion); when the free©/delete (C++) function is called to release the memory, the released memory is removed from the heap Medium elimination (reduction).

6. Memory mapped segment (shared library)

Here, the kernel directly maps the contents of the hard disk file to the memory, and any application can request this mapping through the mmap() system call of Linux. Memory mapping is a convenient and efficient way of file I/O, so it is used to load dynamic shared libraries. For example, C standard library functions (fread, fwrite, fopen, etc.) and Linux system I/O functions, they are all dynamic library functions, of which C standard library functions are encapsulated in the /lib/libc.so library file, both binary file. These dynamic library functions are all position-independent codes, that is, their positions are different each time they are loaded into the memory mapping area, so they use their own logical addresses, which are transformed into linear addresses (virtual addresses). Then map to memory. The static library is different. Because the static library is linked to the executable file, it is located in the code segment, and its position in the address space is fixed each time .

7. Stack space

Used to store local variables (non-static local variables, called automatic variables in C language), from top to bottom when allocating storage space. Both the stack and the heap are last-in first-out data structures.

8. Command line parameters

This section is used to store the content of command line parameters: argc and argv.

9. Environment variables

Used to store the current environment variables, you can use the env command in Linux to view their values.

10. The role of virtual address space (benefits)

1. The compiler and operating system arrange the address of the program; 2. It is convenient to realize the isolation between the space of each process without interfering with each other, because each process corresponds to its own virtual address space; 3. Realize virtual storage, logically Expanded memory.

to add on:

The code segment (.text segment), the read-only data segment and the symbol segment (.rodata segment) are all parts that can only be read. When linking, the two parts will be linked into a whole; while the .data and .bbs segments Belongs to the readable and writable RW part. These four parts are stored in memory in the form of pages (4KB per page). The process control block PCB (also called process descriptor) is placed in the kernel space.

Reprinted from: https://blog.csdn.net/qq_33883085/article/details/88430087

Guess you like

Origin blog.csdn.net/weixin_52270223/article/details/115125675