什么是内存堆?
当前回答
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.
其他回答
内存堆是保存动态分配内存的常用结构。 参见维基百科上的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.
您可能指的是堆内存,而不是内存堆。
堆内存本质上是一个大内存池(通常是每个进程),运行中的程序可以从中请求块。这通常称为动态分配。
它不同于Stack,后者是分配“自动变量”的。因此,例如,当你在C函数中定义一个指针变量时,在堆栈上分配了足够的空间来保存内存地址。但是,您经常需要在堆上动态分配空间(使用malloc),然后向指针提供该内存块开始的地址。
内存组织分为两部分:堆内存和堆栈内存。
堆内存是主工作内存,最低地址是起始地址。
在堆栈内存中,数据流是由自下而上的方法驱动的。然后内存Arch被命名为stack。
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.