为什么两个不同的概念都称为“堆”?

发布于 2024-08-11 06:07:00 字数 132 浏览 12 评论 0原文

为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

千鲤 2024-08-18 06:07:01

问:什么是堆?
答:堆是彼此叠放的对象的集合。

回答你的问题:
如您所知,内存堆和二进制堆都使用相同的概念。
数据以堆的形式存储在内存中,顺序与程序中写入的顺序相同,而二叉堆是一种遵循与以堆的形式有序存储数据相同概念的数据结构(数据在顶部)另一个)。
请在评论部分告诉我你的想法。

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.

哥,最终变帅啦 2024-08-18 06:07:01

也许第一个实现的内存堆是由堆结构管理的?

Perhaps the first memory heap implemented was managed by a heap structure?

月下伊人醉 2024-08-18 06:07:00

Donald Knuth 说(《计算机编程的艺术》,第三版,第 1 卷,第 435 页):

一些作者大约从 1975 年开始将可用内存池称为“堆”。

他没有说明作者是谁,也没有引用任何特定论文,但确实表示与优先级队列相关的术语“堆”的使用是该词的传统含义。

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."

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.

躲猫猫 2024-08-18 06:07:00

它们具有相同的名称,但实际上并不相似(甚至在概念上)。内存堆被称为堆,就像您将洗衣篮称为“衣服堆”一样。这个名字用来表示一个有点混乱的地方,可以随意分配和释放内存。数据结构(正如您引用的维基百科链接指出的那样)非常不同。

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.

云淡风轻 2024-08-18 06:07:00

名称冲突是不幸的,但也不那么神秘。 是一个小而常见的词,用于表示堆、集合、组等。该词在数据结构中的使用早于(我很确定)池的名称的记忆。事实上,在我看来,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

故事与诗 2024-08-18 06:07:00

实际上,阅读有关内存分配方式的信息(请参阅 Buddy Blocks)让我想起了堆在数据结构中。

Actually, reading about the way memory is allocated (see Buddy Blocks) reminds me of a heap in data structures.

一个人练习一个人 2024-08-18 06:07:00

查找可用内存分配的算法使用类似堆的数据结构。以下内容摘自http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html

当调用new时,它开始寻找适合您请求大小的空闲内存块。假设找到这样的内存块,它将被标记为保留并返回指向该位置的指针。有多种算法可以实现此目的,因为必须在扫描整个内存以查找大于对象大小的最小空闲块或返回所需内存适合的第一个块之间做出折衷。为了提高获取内存块的速度,内存的空闲区域和保留区域都以类似于二叉树的数据结构(称为堆)进行维护。

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.

When new is invoked, it starts looking for a free memory block that fits the size for your request. Supposing that such a block of memory is found, it is marked as reserved and a pointer to that location is returned. There are several algorithms to accomplish this because a compromise has to be made between scanning the whole memory for finding the smallest free block bigger than the size of your object, or returning the first one where the memory needed fits. In order to improve the speed of getting a block of memory, the free and reserved areas of memory are maintained in a data structure similar to binary trees called a heap.

不一样的天空 2024-08-18 06:07:00

在我看来,这两个完全不相关的事物具有相同的名称只是一个意外/巧合。它就像图表图表

IMO it is merely an accident/coincidence that these two entirely unrelated things have the same name. Its like graph and graph.

姐不稀罕 2024-08-18 06:07:00

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文