A heap is a tree data structure where higher levels of the tree always contain greater (or lesser, if it's set up that way) values than lower levels.
Donald Knuth says (The Art of Computer Programming, Third Ed., Vol. 1, p. 435):
Several authors began about 1975 to call the pool of available memory a 'heap.' But in the present series of books, we will use that word only in its more traditional sense related to priority queues.
"The" heap is a bunch of free RAM that a program has available for dynamic allocation.
还有两幅形象的图片对比heap和stack
- stack(In a stack of items, items sit one on top of the other in the order they were placed there, and you can only remove the top one (without toppling the whole thing over).)

- heap(In a heap, there is no particular order to the way items are placed.
You can reach in and remove items in any order because there is no clear
'top' item.)

从百度百科上看到一句话,“由于Stack的内存管理是顺序分配的,而且定长,不存在内存回收问题;而Heap 则是随机分配内存,不定长度,存在内存分配和回收的问题;” Why?
这个问题需要测试一下。
本文详细解释了计算机科学中堆(heap)与栈(stack)的概念及它们之间的区别。通过类比的方式,帮助读者理解这两种数据结构的特点:栈遵循后进先出的原则,而堆则没有固定的取出顺序。此外,还探讨了两者在内存管理方面的不同之处。
387

被折叠的 条评论
为什么被折叠?



