Introduction:
A well-written program is generally stored in memory, then the code, data and other parts of the program, how to regularly stored in the memory it?
A
A stored program can be divided into five parts: body segment, initialization data segment, non-initialized data segment, stack, heap . Its typical storage arrangements are as follows:
Body segment: This is the part of the machine instruction that is executed by the CPU. Normally, body segments are shareable, so even frequently executed programs require only one copy in memory, and the body segment is often read-only to prevent the program from modifying its own instructions because of an unexpected change.
Initialization data segment: typically called a data segment, it contains variables that need to be explicitly assigned to the initial value in the program.
Non-initialized data segments: typically called BSS segments. The kernel initializes the data in this segment to a 0 or null pointer before the program begins execution.
Stacks: Automatic variables and the information you need to save each time the function is called are stored in this section. Each time the function is called, its return address and the caller's environment information are stored in the stack. The recently called function then allocates storage space for its automatic and temporary variables on the stack.
Heap: Dynamic storage allocation is typically performed in the heap.
Use the size command in Linux to view the length of the body segment, data segment, and BSS segment of a file.
Two
Memory allocation: The functions used for dynamic allocation of storage space are malloc/calloc/realloc.
1. malloc: Allocates the store with the specified number of bytes. The initialization value in this store is not deterministic.
2. Calloc: Allocates storage space for the specified number of objects. Each bit in the space is initialized to 0.
3. ReAlloc: Change the length of the previously allocated area (increase or decrease).
The advantages of dynamic memory management: the ability to dynamically meet the needs of memory space.
Three
Shared libraries: shared libraries make it no longer necessary to include common library routines in the executable, but to maintain a copy of the library routine in a store that all processes can reference. when the program first executes or when a library function is called for the first time, a dynamic link method is used to link the program to the shared library function. This reduces the length of each executable file, but adds some run-time overhead. Another advantage of shared libraries is that you can replace older versions with new versions of library functions without needing to reconnect edits to programs that use the library.