为什么在c风格的语言中运行时堆被用于动态内存分配,而数据结构都被称为“堆”?有什么关系吗?
当前回答
问:堆是什么? A.堆是放在彼此上面的对象的集合。
回答你的问题: 内存堆和二进制堆都使用相同的概念。 数据以堆的形式存储在内存中,顺序与程序中写入的顺序相同,而二进制堆是一种数据结构,它遵循以堆的形式有序存储数据的相同概念(数据在其他数据之上)。 请在评论区告诉我你的想法。
其他回答
算法采用堆式数据结构查找可用内存分配。以下内容摘自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++标准中并没有使用通俗的术语堆栈内存和堆内存。该标准使用静态存储、线程存储、自动存储和动态存储。
更多信息可以在标准的存储时间部分找到。
因此,从语言和标准库的角度来看,不存在混淆。
它们有相同的名字,但实际上并不相似(甚至在概念上)。内存堆被称为堆,就像你把洗衣篮称为“一堆衣服”一样。这个名字用来表示一个有点混乱的地方,可以随意分配和释放内存。数据结构(正如你参考的维基百科链接所指出的)是完全不同的。
在我看来,这两个完全不相关的东西有相同的名字只是一个意外/巧合。就像图和图。
推荐文章
- 如何通过参数和重定向stdin从一个文件到程序运行在gdb?
- 为什么两个不同的概念都叫“堆”?
- 为什么这个结合赋值和相等检查的if语句返回true?
- 如何找到Java堆大小和内存使用(Linux)?
- cplusplus.com给出的错误、误解或坏建议是什么?
- 找出质数最快的算法是什么?
- c++枚举类可以有方法吗?
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- 将析构函数设为私有有什么用?
- main()中的Return语句vs exit()
- 为什么c#不提供c++风格的'friend'关键字?
- 在函数的签名中添加关键字
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 为什么在标准容器中使用std::auto_ptr<>是错误的?
- 用比较double和0