什么是内存堆?
想必您是指从内存分配的角度来看堆,而不是从数据结构的角度来看堆(这个术语有多种含义)。
一个非常简单的解释是,堆是动态分配内存所在的内存部分(即通过malloc分配的内存)。从堆中分配的内存将保持分配状态,直到发生以下情况之一:
内存已释放 程序终止
如果所有对已分配内存的引用都丢失了(例如,你不再存储指向它的指针),你就有了所谓的内存泄漏。这是仍然分配内存的地方,但是您没有简单的方法来访问它了。泄漏的内存不能被回收用于将来的内存分配,但是当程序结束时,内存将被操作系统释放。
与此形成对比的是堆栈内存,堆栈内存是存放局部变量(在方法中定义的变量)的地方。在堆栈上分配的内存通常只在函数返回之前存在(有一些例外,例如静态局部变量)。
您可以在本文中找到关于堆的更多信息。
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.
您可能指的是堆内存,而不是内存堆。
堆内存本质上是一个大内存池(通常是每个进程),运行中的程序可以从中请求块。这通常称为动态分配。
它不同于Stack,后者是分配“自动变量”的。因此,例如,当你在C函数中定义一个指针变量时,在堆栈上分配了足够的空间来保存内存地址。但是,您经常需要在堆上动态分配空间(使用malloc),然后向指针提供该内存块开始的地址。
every running process has its own private fake virtual memory provided by the OS. the OS can map this to physical memory at any point as long as it is available otherwise it will map to disk and swap as needed. this virtual memory is logically divided into segments for organizing different kinds of data. the code segment holds the executable instructions. the data segment holds static data such as global or static variables. the stack holds local data that is automatically managed by called and returning functions. all of these segments are fixed size even the stack its just the portion used can grow or shrink and is reclaimed as functions returned. the only segment that is not preallocated at app startup and fixed size is the heap. the app can request from the OS at runtime new memory to be allocated and the OS will reserve a part of your apps virtual space and commit that to physical memory as needed. the OS will return a pointer to that newly allocated heap memory and that pointer holds the base or starting address of the new block. that pointer sits on the stack and when that stack space is reclaimed your pointer will be no longer in scope and therefore you have no means of access to that block of memory. and if you dont tell the OS you are done with it so it can reclaim it that is just zombie memory sitting there with no means of access and if your app keeps requesting memory while never giving it back it will crash when the system runs out of memory. so it is important to free or at least pass the pointer to another pointer external to the scope it was defined in so you can maintain an interface to that memory allocated in heap space. i would suggest looking into virtual memory further and understanding segments.
内存组织分为两部分:堆内存和堆栈内存。
堆内存是主工作内存,最低地址是起始地址。
在堆栈内存中,数据流是由自下而上的方法驱动的。然后内存Arch被命名为stack。