memory space divided into several parts: code snippet, data segment, stack, heap (collect and organize) 1. The function code is stored in the code snippet. If the declared class is never used, it is optimized at compile time, and its member functions do not occupy the code segment space. global variable or static variable, placed in the data segment, Local variables are placed in the stack, The objects created with new are placed in the heap,
Memory is divided into 4 segments, stack area, heap area, code area, global variable area BSS segment: BSS segment (BSS segment) usually refers to an area of memory that is used to store uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations.
2. Code snippets, data segments, stacks are CPU-level logical concepts, and heaps are logical concepts at the language level
3. There is also a constant area where the contents are not allowed to be modified. Common char *p = "Hello"; This "Hello" is stored in the constant area.
4. As stated on the 1 floor, it is not appropriate to tie together code snippets, data segments, stacks, and stacks. Code snippets, data segments, stack segments, this is a concept Heap, stack, global zone, constant area, this is another concept
5.STACK (stack) temporary partial Heap Dynamic RW (Read-write) global RO (read-only) code char* s= "Hello,world"; The "H" in S is stored in the memory ro and cannot be modified.
6.CPU Registers: The CPU registers, actually is to control the code snippet and the data section instruction and the data reads the place, of course, the CPU also has its own storage data place, that is the general register the data register, is usually the edx register, C language has a register, is to put the data in this register, so that the reading of the data is quite fast, because no memory to find, it saves the time to address and transmit data overhead. He also has some registers that indicate the position of the current code snippet, the position of the data segment, the position of the stack segment, and so on (note that there is only the corresponding code or data in memory address, not the actual value, and then according to this address, through the address bus and the bus, to get the corresponding value in memory), Otherwise, when executing the code, where are the instructions and data taken from? Oh... He also has a sign register that identifies some status bits, such as an arithmetic overflow and so on. ———————————————————————————————————————————————————————————————— Memory fragmentation
In the architecture of von Neumann, there must be: Code snippets, stack segments, data segments Because Fengshi structure, the essence is to take the site, the process of execution
The compiler and system are assigned from a high address when assigning variables. The storage of global variables and function parameters in memory is from lowland values to high addresses. Why is the function parameter put into the heap area? This is because our function is called dynamically during the execution of the program. At the compile stage of the function, it is impossible to determine how much memory he will call several times. Even if you can be sure that allocating memory for a variable at that time is really a waste. So the compiler chooses the dynamic assignment for the function parameter: That is, each time the function is called, it is dynamically allocated space.
####################################################
Memory is divided into 4 segments, stack area, heap area, code area, global variable area
BSS segment: BSS segment (BSS segment) usually refers to an area of memory that is used to store uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations.
Data segment: Data segment usually refers to an area of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation.
Code Snippets: Code segment/text segment usually refers to an area of memory that is used to store program execution code. The size of this area is determined before the program runs, and the memory area is usually read-only, and some schemas allow the code snippet to be writable, which allows the program to be modified. In a code snippet, it is also possible to include some read-only constant variables, such as String constants. The code snippet is the data that holds the program code, If there are several processes in the machine running the same program, then they can use the same code snippet.
Heap: A heap is used to store a dynamically allocated memory segment in a process run, which is not fixed in size. Can be dynamically expanded or reduced. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded); The freed memory is removed from the heap (heap is scaled down) when the memory is freed using functions such as free Stack: Stacks are also called stacks, which are local variables that are created temporarily by the user-store program. That is, we function the variables defined in brackets "{}" (but do not include variables of static declarations, static means storing variables in the data segment). In addition, when a function is called, its arguments are also pressed into the process stack that initiates the call, and the return value of the function is stored back to the stack when the call ends. Due to the advanced first-out features of the stack, the stack is particularly handy for saving/recovering call sites. In this sense, we can think of the stack as a memory area where temporary data is stored and exchanged.
(1) Memory segmentation and memory paging is a memory management technology, segmentation: rights protection, paging: virtual memory.
(2) After the segmentation, the programmer can define their own segments, each segment has a separate address space, as the process of the address space is independent of each other.
(3) An instance of the same class is assigned to a segment, and only the methods of that class can be accessed, and if the methods of other classes are accessed, an error occurs because of the protection of the segment. Data protection and hiding of classes can be implemented from hardware
####################################################################
Segmentation Benefits:
The segment register in the CPU-------the upper limit (limit) of the segment address (base) and the offset value. Segment Address: An exception is thrown if the valid address is greater than limit. This restricts the program from data outside the current segment and cannot access data from other programs. Object-oriented Benefits: An object is a contiguous piece of data in memory
Registers are special forms of memory that are embedded inside the processor.
Each process needs to access an area of its own in memory, so the memory can be divided into small segments and distributed to processes on demand. Registers are used to store and track segments currently maintained by the process. Offset registers is used to track where critical data is placed in segments.
When a process is loaded into memory, it is basically split into a number of small nodes (sections). We are more concerned with 6 major sections:
(1). Text section
The. Text section is basically the. Text portion of the binary executable file, which contains the machine instructions to complete the program task. The section is marked as read-only and if a write operation occurs, it causes segmentation fault. The size of the section is fixed when the process is initially loaded into memory.
(2). Data section
The. Data section is used to store initialized variables, such as: int a = 0; The size of the section is fixed at run time.
(3). BSS section
The lower section of the stack (below stack sections, or. BSS) is used to store variables that are initialized, such as: int A; The size of the section is fixed at run time.
(4) Heap Section
The heap section is used to store dynamically allocated variables, where the location grows from the low address of memory to the high address. The allocation and release of memory is controlled by the malloc () and free () functions.
(5) Stack section
The stack section is used to track function calls (which may be recursive) and to grow from the high address of memory to the low address on most systems. At the same time, this growth mode of stack leads to the possibility of buffer overflow.
(6) Environment/parameter section
The Environment/parameter section (environment/arguments) is used to store a copy of the system environment variable, Processes may be required at run time. For example, a running process can access information such as the path, Shell name, host name, and so on through environment variables. This section is writable, so it can be used in both the format string and buffer overflow (overflow) attacks. In addition, command-line arguments remain in the zone.
################################################################################
Take the Win32 procedure as an example. When the program executes, the operating system maps the EXE file into memory. EXE file format is composed of header data and individual pieces of data.
The header data describes the EXE file's properties and execution environment, segment data is divided into data section, code snippet, resource section, the number of segments and location of the data description. That is, not just code snippets and data segments. These segments are controlled by different compilation environments and compilation parameters, and the compiler automatically generates EXE segments and file formats. When the operating system executes an EXE, the stack segment is dynamically built, dynamic, and belongs to the operating system execution environment.
In other words, the program's in-memory mapping is an EXE file mapping, including data segments, code snippets, and so on. The other is the stack segment, which is dynamically changed with the program running.
1. The compiler translates the source code into a discrete object code (. O or. obj) file in which the code is already an executable machine code or an intermediate code. But the address of something like variables is just a few symbols. 2. The next step is to process the target code through the linker, the main purpose is to connect the discrete target code into a complete executable code, and replace the address symbol with a relative address. If this is an error, we can get a list of address symbols instead of a list of variables. 3. When executing the program, the operating system allocates enough memory space, establishes the system support structure, and reads the binary executable code into memory. During the read-in process, the memory header becomes the "absolute address" of the program (in fact, the relative address, but the relative address in the operating system). Then the absolute address + relative address (that is, the offset) Gets the address of the variable. Therefore, the value of CS is populated by the system, while the values of the other s registers are calculated from the information appended to the program code.
BSS segment: A BSS segment (BSS segment) usually refers to an area of memory that is used to hold uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol. BSS segments belong to static memory allocations. * BSS (Block Started by Symbol)-uninitialized data(kernel) 101 * zeroed during startup
Data segment: data segment usually refers to an area of memory that is used to hold the initialized global variables in the program. The data segment belongs to static memory allocation. Code Snippets: code segment/text segment usually refers to an area of memory that is used to store program execution code. The size of this area is determined before the program runs, and the memory area is usually read-only, and some schemas allow the code snippet to be writable, which allows the program to be modified. In a code snippet, it is also possible to include some read-only constant variables, such as String constants. Heap: A heap is used to store a dynamically allocated memory segment in a process run, which is not fixed in size and can be dynamically expanded or scaled down. When a process calls a function such as malloc to allocate memory, the newly allocated memory is dynamically added to the heap (heap is expanded), and freed memory is removed from the heap when memory is freed (heap is scaled down). Stack: Stacks are also called stacks, which are local variables that are temporarily created by the user's program, that is, the variables defined in the parentheses "{}" (but not the variables that are static declarations, static means that the variables are stored in the data segment). In addition, when a function is called, its arguments are also pressed into the process stack that initiates the call, and the return value of the function is stored back to the stack when the call ends. Due to the advanced first-out features of the stack, the stack is particularly handy for saving/recovering call sites. In this sense, we can think of the stack as a memory area where temporary data is stored and exchanged.
|