The difference between a "turn" heap and a stack

Source: Internet
Author: User

Heap and Stack (stack) are the two basic concepts that are inevitably encountered in C/s + + programming. First of all, these two concepts can be found in the Book of data structure, they are the basic structure, although the stack is simpler.

These two concepts are not parallel in a specific C + + programming framework. The study of the underlying machine code reveals that the stack is the data structure provided by the machine system, while the heap is provided by the C/S function library.

Stacks and heaps are actually understood in two ways:

One, the pure understanding of the data structure: the stack is an advanced out of the linear table, as long as it conforms to the principle of advanced after the linear table is a stack, the use of arrays or lists to achieve linear table is not related. Heap is a two-fork tree, with the so-called maximum heap minimum heap, sorting algorithm has a common heap sorting.

Second, the system angle of understanding: stack, the system for the running program allocation of the last-in-first-out storage area (play the assembly, it is necessary to know that each program has so-called stack segment), generally used to save local variables or function parameters. Heap, a system-managed global storage space that can be used by programs, typically to hold global variables and static local variables, and the dynamically allocated memory of pointers from the heap.

Specifically, the modern computer (serial execution mechanism) directly supports the data structure of the stack at the bottom of the code. This is reflected in, there is a dedicated register to the address of the stack, there is a dedicated machine instruction to complete the data into the stack out of the stack operation. This mechanism is characterized by high efficiency, limited data support, generally integer, pointer, floating point and other systems directly supported by the data type, does not directly support other data structures. Because of this feature of the stack, the use of stacks is very frequent in the program. The call to the child program is done directly using the stack. The call instruction of the machine implies that the return address is pushed into the stack and then jumps to the subroutine address, while the RET instruction in the subroutine implicitly pops the return address from the stack and jumps to the operation. The automatic variable in C + + is an example of using the stack directly, which is why the automatic variable of the function is automatically invalidated when the function returns (because the stack recovers the state before the call).

Unlike stacks, the data structure of a heap is not supported by the system (whether it is a machine system or an operating system), but is provided by a library of functions. The basic Malloc/realloc/free function maintains a set of internal heap data structures. When the program uses these functions to obtain new memory space, the function first tries to find the available memory space from the internal heap, if there is no memory space available, then attempts to use the system to dynamically increase the size of the program data segment memory, the newly allocated space is first organized into the internal heap, It is then returned to the caller in the appropriate form. When the program frees the allocated memory space, this memory space is returned to the internal heap structure and may be appropriately processed (such as merging additional free space into a larger free space) to better fit the next memory allocation request. This complex allocation mechanism is actually equivalent to a memory allocation buffer pool (cache), which has several reasons for using this set of mechanisms:

1. System calls may not support memory allocations of any size. Some system calls only support a fixed size and multiple memory requests (per page allocation), which can be wasteful for a large number of small memory classifications.

2. System calls to request memory can be costly. System calls may involve conversion of the user state and kernel mentality.

3. Memory allocations that are not managed can easily cause memory fragmentation under the allocation release operation of a large amount of complex memory.

Comparison of heaps and stacks

From the above knowledge, the stack is the function provided by the system, the characteristics are fast and efficient, the disadvantage is limited, the data is not flexible, and the heap is the function library provides the function, the characteristic is flexible and convenient, the data adapts widely, but the efficiency has a certain reduction. A stack is a system data structure that is unique to a process/thread, and a heap is a library internal data structure that is not necessarily unique. Memory allocated by different heaps cannot operate with each other. The stack space is divided into two kinds: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the automatic variable (auto) assignment. Dynamic allocation is done by the Alloca function. The dynamic allocation of the stack does not have to be released (it is automatic), and there is no release function. For portable programs, the dynamic allocation of stacks is not encouraged! The allocation of heap space is always dynamic, although all data space will be released back to the system at the end of the program, but an accurate memory/release memory match is an essential element of a good program.

The difference between a "turn" heap and a stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.