为什么在c风格的语言中运行时堆被用于动态内存分配,而数据结构都被称为“堆”?有什么关系吗?


当前回答

问:堆是什么? A.堆是放在彼此上面的对象的集合。

回答你的问题: 内存堆和二进制堆都使用相同的概念。 数据以堆的形式存储在内存中,顺序与程序中写入的顺序相同,而二进制堆是一种数据结构,它遵循以堆的形式有序存储数据的相同概念(数据在其他数据之上)。 请在评论区告诉我你的想法。

其他回答

在我看来,这两个完全不相关的东西有相同的名字只是一个意外/巧合。就像图和图。

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

1975年左右,一些作者开始将可用内存池称为“堆”。

他没有说哪些作者,也没有给出任何特定论文的参考文献,但他确实说了,与优先级队列相关的术语“堆”的使用是这个词的传统意义。

实际上,读到内存分配的方式(参见Buddy Blocks)让我想起了数据结构中的堆。

算法采用堆式数据结构查找可用内存分配。以下内容摘自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.

问:堆是什么? A.堆是放在彼此上面的对象的集合。

回答你的问题: 内存堆和二进制堆都使用相同的概念。 数据以堆的形式存储在内存中,顺序与程序中写入的顺序相同,而二进制堆是一种数据结构,它遵循以堆的形式有序存储数据的相同概念(数据在其他数据之上)。 请在评论区告诉我你的想法。