&是什么QUOT;单个分配"意味着升压:: make_shared [英] What does "single allocation" mean for boost::make_shared
问题描述
在 make_shared 升压文档,它说:
除了方便和风格,这样的功能也异常安全,大大
更快,因为它可以使用单一分配为对象及其对应的
控制块,消除shared_ptr的建设开销显著部分。
Besides convenience and style, such a function is also exception safe and considerably faster because it can use a single allocation for both the object and its corresponding control block, eliminating a significant portion of shared_ptr's construction overhead.
我不明白单分配的含义,这是什么意思?
I don't understand the meaning of "single allocation", what does it mean?
推荐答案
这是分配是指从一个呼叫分配器获得的内存块。
An "allocation" means a block of memory obtained from a call to an allocator.
通常情况下,创建一个的shared_ptr
用指针构造一个控制块,其中包含引用计数和删除器分配内存。该的shared_ptr
所有的副本指的是同一控制块,让他们共享的引用计数。因此,总共有两笔拨款 - 对象本身和的shared_ptr
创建了控制块
Usually, creating a shared_ptr
with the pointer constructor allocates memory for a "control block", which holds the reference count and the deleter. Copies of that shared_ptr
all refer to the same control block, so that they share the reference count. Hence there are two allocations in total - the object itself and the control block created by shared_ptr
.
如果您创建对象和的shared_ptr
连同 make_shared
,那么只有一个分配的。你可以认为这是一个单一的结构有两个成员:
If you create the object and shared_ptr
together with make_shared
, then only one allocation is made. You can think of this as a single struct with two members:
- 这是被管理 对象
- 控制块。
这篇关于&是什么QUOT;单个分配"意味着升压:: make_shared的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!