Memory partition and block storage location

Memory partition:

1. The stack area (stack) is automatically allocated and released by the compiler, and stores the parameter values ​​of the function, local variables, etc. The stack is a system data structure, and the corresponding thread/process is unique.
    The advantages are fast and efficient, the disadvantages are sometimes limited, and the data is not flexible.

    PS: A large number of local variables, deep recursion, and function loop calls may exhaust memory and cause the operation to crash

2. The heap area is allocated and released by the programmer. If the programmer does not release it, it may be reclaimed by the operating system when the program ends. For example, in ios, alloc is stored in the heap.

     The memory allocated by alloc is relatively slow and prone to memory fragmentation, but it is the most convenient to use.

3. Global area (static area) (static) Global variables and static variables are stored together, initialized global variables and static variables are stored in one area, and uninitialized global variables and static variables are in another adjacent block. area, which is released by the system after the program ends.
Note: The global area can be further divided into uninitialized global area: .bss segment and initialized global area: data segment.

4. The literal constant area stores constant character strings, which are released by the system after the program ends.

5. The program code area stores the binary code of the function.

The storage location of the block:

The block block has two possible storage locations according to the situation, one exists in the code area, and the other exists in the heap area.

1. If the block block does not access variables in the stack area (such as local variables), nor does it access variables in the heap area (such as objects created by our alloc), then there is a code area. Even if the global variables are accessed, there is still code. Area.

2. If a variable in the stack area or heap area is accessed, it will be stored in the heap area (the stack area actually exists, and it will be automatically copied to the heap area under ARC).

Guess you like

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