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


当前回答

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

其他回答

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

在c++标准中并没有使用通俗的术语堆栈内存和堆内存。该标准使用静态存储、线程存储、自动存储和动态存储。

更多信息可以在标准的存储时间部分找到。

因此,从语言和标准库的角度来看,不存在混淆。

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

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

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

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

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