What is the difference between heap and stack

1. The difference in stack space allocation

Stack (operating system): automatically allocated and released by the operating system (compiler), storing function parameter values, local variable values, etc. Its operation is similar to the stack in the data structure.

Heap (operating system): The memory block allocated by new, malloc, and realloc is generally allocated and released by the programmer. If the programmer does not release it, the OS may reclaim it at the end of the program. The allocation method is similar to a linked list.

2. The difference between stack caching methods

The stack uses the first level cache. They are usually in the storage space when they are called, and they are released immediately after the call.

The heap is stored in the second-level cache, and its life cycle is determined by the garbage collection algorithm of the virtual machine (not that it can be recycled once it becomes an orphan object). Therefore, the speed of calling these objects is relatively low.

3. The difference in stack data structure

Heap (data structure): Heap can be regarded as a tree, such as: heap sort.

Stack (data structure): a first-in-last-out data structure.

Guess you like

Origin blog.csdn.net/kevlin_V/article/details/107016784