2024-12-11 08:00:04

什么是内存堆?

什么是内存堆?


当前回答

A memory heap is a location in memory where memory may be allocated at random access. Unlike the stack where memory is allocated and released in a very defined order, individual data elements allocated on the heap are typically released in ways which is asynchronous from one another. Any such data element is freed when the program explicitly releases the corresponding pointer, and this may result in a fragmented heap. In opposition only data at the top (or the bottom, depending on the way the stack works) may be released, resulting in data element being freed in the reverse order they were allocated.

其他回答

堆只是一个没有任何顺序地分配或释放内存的区域。这种情况发生在使用new操作符或类似操作符创建对象时。这与按先入后出原则释放内存的堆栈相反。

想必您是指从内存分配的角度来看堆,而不是从数据结构的角度来看堆(这个术语有多种含义)。

一个非常简单的解释是,堆是动态分配内存所在的内存部分(即通过malloc分配的内存)。从堆中分配的内存将保持分配状态,直到发生以下情况之一:

内存已释放 程序终止

如果所有对已分配内存的引用都丢失了(例如,你不再存储指向它的指针),你就有了所谓的内存泄漏。这是仍然分配内存的地方,但是您没有简单的方法来访问它了。泄漏的内存不能被回收用于将来的内存分配,但是当程序结束时,内存将被操作系统释放。

与此形成对比的是堆栈内存,堆栈内存是存放局部变量(在方法中定义的变量)的地方。在堆栈上分配的内存通常只在函数返回之前存在(有一些例外,例如静态局部变量)。

您可以在本文中找到关于堆的更多信息。

内存堆是保存动态分配内存的常用结构。 参见维基百科上的Dynamic_memory_allocation。

还有其他结构,比如水池、堆栈和堆。

A memory heap is a location in memory where memory may be allocated at random access. Unlike the stack where memory is allocated and released in a very defined order, individual data elements allocated on the heap are typically released in ways which is asynchronous from one another. Any such data element is freed when the program explicitly releases the corresponding pointer, and this may result in a fragmented heap. In opposition only data at the top (or the bottom, depending on the way the stack works) may be released, resulting in data element being freed in the reverse order they were allocated.

内存组织分为两部分:堆内存和堆栈内存。

堆内存是主工作内存,最低地址是起始地址。

在堆栈内存中,数据流是由自下而上的方法驱动的。然后内存Arch被命名为stack。