为什么两个不同的概念都称为“堆”?
为什么 C 风格语言中使用运行时堆进行动态内存分配以及数据结构 都称为“堆”?有什么关系吗?
Why are the runtime heap used for dynamic memory allocation in C-style languages and the data structure both called "the heap"? Is there some relation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
问:什么是堆?
答:堆是彼此叠放的对象的集合。
回答你的问题:
如您所知,内存堆和二进制堆都使用相同的概念。
数据以堆的形式存储在内存中,顺序与程序中写入的顺序相同,而二叉堆是一种遵循与以堆的形式有序存储数据相同概念的数据结构(数据在顶部)另一个)。
请在评论部分告诉我你的想法。
Q. What is a heap?
A. A heap is a collection of objects place on top of each other.
Answer to your question:
Both memory heap and binary heap uses the same concept as you know.
Data is stored in the form of a heap in the memory in the same order as written in the program whereas binary heap is a data structure that follows the same concept of storing data in an ordered way in the form of a heap(Data on top of the other).
Let me know what you think in the comments section.
也许第一个实现的内存堆是由堆结构管理的?
Perhaps the first memory heap implemented was managed by a heap structure?
Donald Knuth 说(《计算机编程的艺术》,第三版,第 1 卷,第 435 页):
他没有说明作者是谁,也没有引用任何特定论文,但确实表示与优先级队列相关的术语“堆”的使用是该词的传统含义。
Donald Knuth says (The Art of Computer Programming, Third Ed., Vol. 1, p. 435):
He doesn't say which authors and doesn't give references to any specific papers, but does say that the use of the term "heap" in relation to priority queues is the traditional sense of the word.
它们具有相同的名称,但实际上并不相似(甚至在概念上)。内存堆被称为堆,就像您将洗衣篮称为“衣服堆”一样。这个名字用来表示一个有点混乱的地方,可以随意分配和释放内存。数据结构(正如您引用的维基百科链接指出的那样)非常不同。
They have the same name but they really aren't similar (even conceptually). A memory heap is called a heap in the same way you would refer to a laundry basket as a "heap of clothes". This name is used to indicate a somewhat messy place where memory can be allocated and deallocated at will. The data structure (as the Wikipedia link you reference points out) is quite different.
名称冲突是不幸的,但也不那么神秘。 堆是一个小而常见的词,用于表示堆、集合、组等。该词在数据结构中的使用早于(我很确定)池的名称的记忆。事实上,在我看来,pool 对于后者来说是更好的选择。 堆表示垂直结构(如堆),它适合数据结构,但不适合内存池。我们不认为内存池堆是分层的,而数据结构背后的基本思想是将最大元素保留在堆(和子堆)的顶部。
堆这种数据结构的历史可以追溯到 60 年代中期;堆内存池,70 年代初。术语堆(意思是内存池)至少早在 1971 年就被 Wijngaarden 在讨论阿尔戈尔。
最早使用堆作为数据结构可能是在七年前的
中发现的
Williams,JWJ 1964。“算法 232 - 堆排序”,ACM 通讯 7(6): 347-348
The name collision is unfortunate, but not all that mysterious. Heap is a small, common word used to mean a pile, collection, group, etc. The use of the word for the data structure pre-dates (I'm pretty sure) the name of the pool of memory. In fact, pool would have been a much better choice for the latter, in my opinion. Heap connotes a vertical structure (like a pile), which fits with the data structure, but not the memory pool. We don't think of a memory-pool heap as hierarchical, whereas the fundamental idea behind the data structure is keeping the largest element at the top of the heap (and sub-heaps).
Heap the data structure dates back to the mid-60s; heap the memory pool, the early-70s. The term heap (meaning memory pool) was used at least as early as 1971 by Wijngaarden in discussions of Algol.
Possibly the earliest use of heap as a data structure is found seven years earlier in
Williams, J. W. J. 1964. "Algorithm 232 - Heapsort", Communications of the ACM 7(6): 347-348
实际上,阅读有关内存分配方式的信息(请参阅 Buddy Blocks)让我想起了堆在数据结构中。
Actually, reading about the way memory is allocated (see Buddy Blocks) reminds me of a heap in data structures.
查找可用内存分配的算法使用类似堆的数据结构。以下内容摘自http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html。
Heap-like data structure is used by algorithm of finding available memory allocation. The following is excerpted from http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html.
在我看来,这两个完全不相关的事物具有相同的名称只是一个意外/巧合。它就像图表和图表。
IMO it is merely an accident/coincidence that these two entirely unrelated things have the same name. Its like graph and graph.
C++ 标准中不使用口语术语“堆栈内存”和“堆内存”。该标准使用静态存储、线程存储、自动存储和动态存储。
更多信息请参阅标准的存储持续时间部分。
因此,从语言和标准库的角度来看,不存在混淆。
The colloquial terms stack memory and heap memory are not used in the C++ standard. The standard uses static storage, thread storage, automatic storage, and dynamic storage.
More can be found at Storage Duraction section of the standard.
Hence, from the language and standard library point of view, there is no confusion.