new和make_shared用于共享指针 [英] new and make_shared for shared pointers

查看:253
本文介绍了new和make_shared用于共享指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到了这篇帖子,其中一篇@kerek SB州的答案

I came across this post and one of the answers by @kerek SB states

std::shared_ptr<Object> p1 = std::make_shared<Object>("foo");
std::shared_ptr<Object> p2(new Object("foo"));

在您的代码中,第二个变量只是一个裸指针,而不是 完全共享指针.

In your code, the second variable is just a naked pointer, not a shared pointer at all.

现在在肉上. make_shared(在实践中)效率更高,因为 它将参考控制块与实际 一个单一动态分配中的对象.相比之下,构造函数 对于使用裸对象指针的shared_ptr,必须分配另一个 用于参考计数的动态变量.权衡是 make_shared(或其堂兄allocate_shared)不允许您执行以下操作: 指定自定义删除器,因为分配是由 分配器.

Now on the meat. make_shared is (in practice) more efficient, because it allocates the reference control block together with the actual object in one single dynamic allocation. By contrast, the constructor for shared_ptr that takes a naked object pointer must allocate another dynamic variable for the reference count. The trade-off is that make_shared (or its cousin allocate_shared) does not allow you to specify a custom deleter, since the allocation is performed by the allocator.

(这不会影响对象本身的构造. Object的观点在两个版本之间没有区别. 更有效率的是共享指针本身,而不是托管指针 对象.)

(This does not affect the construction of the object itself. From Object's perspective there is no difference between the two versions. What's more efficient is the shared pointer itself, not the managed object.)

现在我对这个帖子有两个问题,如果有人可以澄清这个问题,将不胜感激

Now I have two questions regarding this post and would appreciate it if someone could clarify this

  1. 为什么第二个不是共享指针?那不会增加引用计数吗?

  1. Why is the second one not a shared pointer ? Will that not increment a reference count

make_shared如何仅分配一个内存,而new分配两个内存 使make_shared更有效?

How does make_shared only make one memory allocation and new makes two thus making make_shared more efficent ?

对此进行一些澄清将不胜感激.

A little clarification on this would be appreciated.

推荐答案

  1. 被称为第二个变量的代码实际上是这个(摘自OP的代码):

  1. The code referred to as the second variable is in fact this (taken from OP's code):

auto ptr_res2(new Object("new"));

这不会不会创建一个std::shared_ptr,它会创建一个指向Object的指针.

This does not create a std::shared_ptr, it creates a pointer to Object.

使用带裸指针的构造函数创建std::shared_ptr时,必须将指针传递到已分配的内存(例如使用new分配的内存).这意味着在创建std::shared_ptr对象本身时,已经为该对象分配了内存. std::shared_ptr需要为其自身的工作分配内存,例如参考计数器.因此,共有2种分配:一种是使用new传递给std::shared_ptr的ctor的,另一种是构造std::shared_ptr本身所需的.

When creating a std::shared_ptr using its constructor that takes a naked pointer, you must pass a pointer to already allocated memory (e.g. allocated using new). This means that the memory for the object has already been allocated when creating the std::shared_ptr object itself. The std::shared_ptr needs to allocate memory for its own workings, like e.g. a reference counter. So there are 2 allocations: the one using new passed to the ctor of std::shared_ptr and the one required when constructing the std::shared_ptr itself.

Object* ptr = new Object{"Foo"}; // Allocation 1 (object).
std::shared_ptr<Object> p1{ptr}; // Allocation 2 (internal counters).

辅助函数std::make_shared在向其传递构造对象所需的自变量而不是 not 所需的指针时仅使用1种分配对象本身. std::make_shared然后可以一次分配包含对象和引用计数器的内存.

The helper function std::make_shared uses only 1 allocation as you pass it the arguments needed to construct the object, not a pointer to the object itself. std::make_shared can then allocate memory once that holds both the object and the ref counter.

auto p2 = std::make_shared<Object>{"Foo"} // Allocation 1 (object & counter).

这篇关于new和make_shared用于共享指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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