C ++,自由存储vs堆 [英] C++, Free-Store vs Heap

查看:102
本文介绍了C ++,自由存储vs堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new / delete 的动态分配据说发生在免费存储
malloc / free 操作使用



我想知道在实践中是否存在实际差异。

编译器是否区分这两个术语? ( Free store Heap ,而不是 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:

自由存储:


免费存储是两个
动态内存区域之一,通过new / delete分配/释放
。对象生命周期可以是
小于存储空间是
分配的时间;也就是说,自由存储对象
可以分配没有
的内存,并且立即被初始化,并且
可以在不立即释放内存为
的情况下被销毁。在
期间,当存储被分配
但在对象的生命周期之外时,可以访问
存储,并且可以通过原型的void *但不能
来操纵

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和免费特定的
编译器,堆不一样的
在一个
区域中分配的自由存储和内存不能在
中安全地释放另一个。从
堆分配的内存可以通过放置新构造和
显式销毁用于类
类型的对象。如果这样使用,关于免费商店对象生命周期的
注释
在这里也类似。

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堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆