Memory Management - Where to Put Your Data

  1. Uninitialized global variables (.bss)
    The Bss segment is used to store global variables that are not initialized and initialized to 0. This type only occupies the memory space at runtime, and does not occupy the file space of the code code. As a global variable, the bss data always exists throughout the running cycle of the program.
  2. Initialized global variables (.data) The
    Data section is used to store global variables that are initialized to non-zero values. Global variables of type Data occupy both memory space and code code file space. Also as a global variable, the data data always exists throughout the running cycle of the program.
  3. Constant data (.rodata)
    Ro stands for read only, that is, data that cannot be modified, such as const.
    Regarding the rodata type data, pay attention to the following points:
    (1) Constants are not necessarily placed in rodata, and some immediate data are directly encoded with the instruction and stored in the code segment (.text).
    (2) For string constants, the compiler will automatically remove duplicate strings to ensure that only one copy of a string exists in an executable file (exe/so).
    (3) rodata is shared by multiple processes, which improves the utilization of running space.
    (4) In some embedded systems, rodata is placed in ROM (or NOR Flash), which can be read directly at runtime without being loaded into RAM memory.
    (5) In an embedded Linux system, it can also be read directly through a technology called XIP (execution in place) without loading into RAM memory.
    (6) Constants cannot be modified. Modifying constants will cause a segmentation fault under Linux.
  4. Code (.text)
    stores the code code area.
  5. Heap (heap)
    dynamic memory allocation, such as the paired use of malloc and free.

  6. The stack
    is used to store temporary variables and function parameters. Under normal circumstances, the stack grows downward (low address growth), each time an element is pushed to the stack, the top of the stack is extended to a low address, and each time an element is POPed from the stack, the top of the stack is rolled back to a high address.
    Note : The local variables modified by static are also stored in the same location as global variables, in the bss segment and the data segment, rather than in the stack.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325854616&siteId=291194637