C++,自由存储 vs 堆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1350819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:39:30  来源:igfitidea点击:

C++, Free-Store vs Heap

c++memory-management

提问by Nick Dandoulakis

Dynamic allocations with new/deleteare said to take place on the free-store,
while malloc/freeoperations use the heap.

I'd like to know if there is an actual difference, in practice.
Do compilers make a distinction between the two terms? (Free storeand Heap, not new/malloc)

new/delete据说动态分配发生在自由存储上
malloc/free操作使用

我想知道在实践中是否存在实际差异。
编译器会区分这两个术语吗?(免费存储,不是new/malloc

采纳答案by Michael Koval

See http://www.gotw.ca/gotw/009.htm; it can describe the differences between the heap and the free-store far better than I could:

http://www.gotw.ca/gotw/009.htm;它可以比我更好地描述堆和自由存储之间的差异:

Free-store:

免费商店:

The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

free store 是两个动态内存区域之一,由 new/delete 分配/释放。对象生命周期可以小于分配存储的时间;也就是说,自由存储对象可以在不立即初始化的情况下分配内存,并且可以在不立即释放内存的情况下销毁内存。在分配存储但在对象生命周期之外的期间,可以通过 void* 访问和操作存储,但不能访问原型对象的任何非静态成员或成员函数,不能获取其地址,或以其他方式操作.

Heap:

堆:

The heap is the other dynamic memory area, allocated/freed by malloc/free and their variants. Note that while the default global new and delete might be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.

堆是另一个动态内存区域,由 malloc/free 及其变体分配/释放。请注意,虽然默认的全局 new 和 delete 可能由特定编译器根据 malloc 和 free 实现,但堆与空闲存储不同,并且在一个区域中分配的内存不能在另一个区域中安全地释放。从堆分配的内存可以通过放置新构造和显式销毁用于类类型的对象。如果这样使用,关于自由存储对象生命周期的说明在这里同样适用。

回答by Kit10

For C++, the difference between the free store and the heap has become purely conceptual. Like a jar for collecting bugs, and one for collecting cookies. One is labeled one way, the other another. This designation is meant to drive home the point that you NEVER mix "new" and "delete" with "malloc", "realloc", or "free" (or bit level sets for that matter).

对于 C++ 来说,自由存储和堆之间的区别已经变得纯粹是概念上的。就像一个用来收集虫子的罐子,一个用来收集饼干的罐子。一种标记为一种方式,另一种标记为另一种方式。此名称旨在说明您永远不要将“new”和“delete”与“malloc”、“realloc”或“free”(或与此相关的位级别集)混在一起。

During interviews it's good to say that "new and delete use the free store, malloc and free use the heap; new and delete call the constructor and destructor, respectively, however malloc and free do not." Yet, you will often hear that the memory segments are really in the same area - however, that CAN be compiler specific, that is to say, it is possible that both can designate different memory spaces as pools (not sure why it would be, though).

在采访中可以说“new 和delete 使用free store,malloc 和free 使用堆;new 和delete 分别调用构造函数和析构函数,但是malloc 和free 不调用”。然而,您经常会听到内存段实际上位于同一区域 - 然而,这可以是特定于编译器的,也就是说,两者都可能将不同的内存空间指定为池(不知道为什么会这样,尽管)。

回答by Crashworks

Mike Koval's answer covers the theory quite well. In practice, however, they are almost always the same region of memory -- in most cases if you dig into the compiler's implementation of new, you'll find it calls malloc().

Mike Koval 的回答很好地涵盖了这个理论。然而,在实践中,它们几乎总是相同的内存区域——在大多数情况下,如果你深入研究 的编译器实现new,你会发现它调用malloc()

In other words: from the machine's point of view, heap and free store are the same thing. The distinction exists inside the compiler.

换句话说:从机器的角度来看,堆和自由存储是一回事。区别存在于编译器内部。

To make things even more confusing, before the advent of C++ we said "heap" to mean what is now called "free store."

更令人困惑的是,在 C++ 出现之前,我们说“堆”是指现在所谓的“免费存储”。

回答by Jim Lewis

The term "heap" may also refer to a particular data structure, but in the context of the C++ malloc, free, new, and delete operations the terms "heap" and "free store" are used more or less interchangeably.

术语“堆”也可以指特定的数据结构,但在 C++ malloc、free、new 和 delete 操作的上下文中,术语“堆”和“自由存储”或多或少可以互换使用。

回答by Ambroise Leclerc

Heap and free-store aren't supposed to be interoperable. In constrained contextes like in AVR 8-bit micro controllers with c++11 Standard Library, they cannot even be used in the same program. Free store and heap do their allocations in the same memory space, overwriting each other structures and data. In this context, Free store is different and incompatible with Heap because the "new/delete free store library" is simpler (and quicker) than the "Malloc/free/realloc/calloc heap library" and thus provides huge memory usage gains to the C++ embedded programmer (in a context where you have only 512 bytes of RAM).

堆和自由存储不应该是可互操作的。在具有 c++11 标准库的 AVR 8 位微控制器等受限上下文中,它们甚至不能在同一个程序中使用。自由存储和堆在相同的内存空间中进行分配,相互覆盖结构和数据。在这种情况下,Free store 是不同的并且与 Heap 不兼容,因为“新建/删除自由存储库”比“Malloc/free/realloc/calloc 堆库”更简单(也更快),从而为C++ 嵌入式程序员(在只有 512 字节 RAM 的上下文中)。

See 8-bit c++11/14 Standard Library at https://github.com/ambroise-leclerc/ETL/tree/master/libstd

请参阅https://github.com/ambroise-leclerc/ETL/tree/master/libstd 上的8 位 c++11/14 标准库

回答by avakar

I don't recall the standard ever mentioning the word heap, except in the descriptions of heap functions like push_heapet al. All dynamic allocations are performed on the free-store.

我不记得标准曾经提到过堆这个词,除了像push_heap等人这样的堆函数的描述。所有动态分配都在自由存储上执行。

回答by Sudipto

Free Store is a pool of un-allocated heap memory given to a program that is used by the program for dynamic allocation during the execution of program. Every program is provided with a pool of un-allocated heap memory that it may utilize during the execution. This pool of available memory is referred to as free store of the program. The allocated free store memory is unnamed.

Free Store 是分配给程序的未分配堆内存池,程序在执行程序期间使用该池进行动态分配。每个程序都有一个未分配的堆内存池,它可以在执行期间使用。这个可用内存池称为程序的空闲存储。分配的空闲存储内存未命名。