问题描述
使用 new/delete
进行动态分配据说发生在 free-store 上,
而 malloc/free
操作使用堆.
我想知道在实践中是否存在实际差异.
编译器会区分这两个术语吗?(免费存储和堆,而不是new/malloc
)
Dynamic allocations with new/delete
are said to take place on the free-store,
while malloc/free
operations 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 store and Heap, not new/malloc
)
推荐答案
参见 http://www.gotw.ca/gotw/009.htm;它比我能更好地描述堆和自由存储之间的差异:
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:
免费商店:
免费商店是两者之一动态内存区域,分配/释放通过新建/删除.对象生命周期可以是少于存储时间分配;即自由存储对象可以不分配内存被立即初始化,并且可以在没有记忆的情况下被摧毁立即解除分配.在此期间分配存储的时间但是在对象的生命周期之外,可以访问存储和通过虚空*操纵但没有原型对象的非静态成员或成员函数可能是访问,获取他们的地址,或以其他方式被操纵.
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.
堆:
堆是另一个动态内存区域,由 malloc/free 分配/释放以及它们的变种.请注意,同时默认的全局新建和删除可能在以下方面实施malloc 和 free 由特定编译器,堆不一样自由存储和内存分配在一个区域不能安全地解除分配另一个.从分配的内存堆可用于类的对象按放置类型 - 新建和显式销毁.如果这样使用,则关于自由存储对象生命周期的注意事项此处同样适用.
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++,自由存储 vs 堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!