C++,自由存储与堆

标签 c++ memory-management

new/delete 的动态分配据说发生在 free-store
malloc/free 操作使用

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

最佳答案

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

免费商店:

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.

堆:

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.

关于C++,自由存储与堆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1350819/

相关文章:

java - 永久代: does the capacity of default value get fixed or will it grow as required?

c++ - 如何关闭由自定义 QItemDelegate::createEditor() 创建的编辑器

C++ 基本类布局

c++ - 网络 I/O 和 C++ 缓冲区的动态与静态内存分配

memory-management - 用于多个进程的内核驱动程序中的内存池

android - 如何降低我的应用程序的内存使用量?

C++ lambda : good reference vs. 错误引用

java - 为什么向下转换在 C++ 中是一种不好的做法,而不是在另一种语言中?

c++ - ofstream 在询问其内容之前创建文件

c# - 垃圾收集如何决定变量的生成