为什么在c风格的语言中运行时堆被用于动态内存分配,而数据结构都被称为“堆”?有什么关系吗?
当前回答
在c++标准中并没有使用通俗的术语堆栈内存和堆内存。该标准使用静态存储、线程存储、自动存储和动态存储。
更多信息可以在标准的存储时间部分找到。
因此,从语言和标准库的角度来看,不存在混淆。
其他回答
实际上,读到内存分配的方式(参见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.
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).
堆的数据结构可以追溯到60年代中期;堆积记忆池,70年代初。术语堆(指内存池)至少早在1971年由Wijngaarden在Algol的讨论中使用。
可能最早使用堆作为数据结构是在 威廉姆斯,1964年。“算法232 -堆排序”,ACM通信7(6):347-348
它们有相同的名字,但实际上并不相似(甚至在概念上)。内存堆被称为堆,就像你把洗衣篮称为“一堆衣服”一样。这个名字用来表示一个有点混乱的地方,可以随意分配和释放内存。数据结构(正如你参考的维基百科链接所指出的)是完全不同的。
在c++标准中并没有使用通俗的术语堆栈内存和堆内存。该标准使用静态存储、线程存储、自动存储和动态存储。
更多信息可以在标准的存储时间部分找到。
因此,从语言和标准库的角度来看,不存在混淆。
推荐文章
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 公共朋友交换成员函数
- 如何在Go中使用c++
- 自定义c++分配器的引人注目的例子?
- RAII和c++中的智能指针
- 如何构建和使用谷歌TensorFlow c++ api
- 堆与二叉搜索树(BST)
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- gcc在哪里查找C和c++头文件?