The difference between memory space distribution and stack

Memory space distribution

Memory model (4G) size
Kernel space (application inaccessible) About 1G
Stack space (local variables) About 3G
Heap space (application memory space (malloc))
Global data space (static) (initialized, uninitialized)
Code segment (read-only data segment)
Starting address 0x00

Data segment = global + read only

The stack area in the memory is at a relatively high address. If the direction of address growth is upward , the stack address grows downward and the heap address grows upward.

Data structure stack

  • Stack : a first-in-last-out data structure
  • Heap : Heap can be regarded as a tree , such as: heap sort

Stack in memory

  • Stack: automatically allocated and released by the operating system (compiler), storing function parameter values, local variables, etc., in the storage space when called, and released immediately after the call.
  • Heap: generally requested, allocated, released, passed, new 、mallocetc. by the programmer

Compare

Recovery method
  • Stack: automatic allocation, automatic recycling, such as defining a char a;

    The space on the stack is automatically allocated and reclaimed automatically, so the life cycle of the data on the stack is only in the running process of the function , it is released after running, and can no longer be accessed.

  • Heap: The programmer applies for the required size space as needed, for example:

    malloc(5 * sizeof(int) );

    Open up a space of the size of 5 plastic data, as long as the programmer does not free()have access to it, but this is dangerous, forgetting to release will cause memory leaks.

Application efficiency
  • Stack: allocated by the system, faster.
  • Stack: The memory allocated by new is generally slow.
Application size
  • Stack: Under Windows, the stack is a data structure that extends to lower addresses and is a continuous memory area. The address at the top of the stack and the maximum capacity of the stack are predetermined by the system. Therefore, the space available from the stack is smaller .
  • Stack: The heap is a data structure that extends to high addresses and is a discontinuous memory distribution. Because the system uses a linked list to store free memory addresses. Therefore, the application space for pile courses is flexible and relatively large.

Guess you like

Origin blog.csdn.net/qq_30722795/article/details/108154129