[英]Calling `shared_ptr.get()` vs copy-initialization, what's the difference?
first version print use_count = 2; 第一版print use_count = 2;
shared_ptr<int> s{make_shared<int>(15)};
auto b = s;
cout<<s.use_count()<<endl;
auto c = s.get();
cout<<s.use_count()<<endl;
cout<<*c<<endl;
second version use_count = 3; 第二版本use_count = 3;
shared_ptr<int> s{make_shared<int>(15)};
auto b = s;
cout<<s.use_count()<<endl;
auto c = s;
cout<<s.use_count()<<endl;
cout<<*c<<endl;
Question: 题:
Every time you make a copy of a shared_ptr the use-count is increased since you then have an additional handle to the underlying resource being tracked. 每次您复制shared_ptr副本时, 使用计数都会增加,因为这样您就可以对正在跟踪的基础资源进行额外的处理。
In order to obtain the value of the underlying pointer, shared_ptr has a member-function named get
. 为了获得基础指针的值, shared_ptr有一个名为
get
的成员函数。 This function will return the address of the tracked resource, it will not create an additional shared_ptr tracking the same pointer. 此函数将返回被跟踪资源的地址,它将不会创建跟踪同一指针的其他shared_ptr 。
In your first snippet you effectively have 2 instances of shared_ptr that refers to the same resource, but in the latter you have three. 在您的第一个代码段中,您实际上有2个shared_ptr实例,它们引用相同的资源,但是在后者中,您有3个实例。
The difference being; 区别在于;
auto a = s
will create a copy of s
, and the type of a
is that of s
(a shared_ptr ) auto a = s
将创建的副本s
,和类型a
是, s
(一个shared_ptr) auto b = s.get ()
will initialize b
with the address of the tracked resource managed by s
, hence it will be a raw-pointer . auto b = s.get ()
将使用s
管理的跟踪资源的地址来初始化b
,因此它将是原始指针 。 If you don't know exactly what you are doing, calling .get ()
might be dangerous, as can be seen in the following snippet. 如果您不确定自己在做什么,则调用
.get ()
可能很危险,如下面的代码片段所示。
int * ptr = nullptr;
{
std::shared_ptr<int> sp { make_shared<int> (15) };
ptr = sp.get ();
}
std::cout << *ptr << std::endl; // (A), dangling pointer
Inside our nested scope we create a shared_ptr named sp
, and we ask it to keep track of an int
with dynamic storage duration created from our call to make_shared
. 在我们的嵌套范围内,我们创建一个名为
sp
的shared_ptr ,并要求它跟踪从对make_shared
的调用创建的具有动态存储持续时间的int
。
We obtain the address of this int
by calling sp.get ()
, and assign it to ptr
. 我们通过调用
sp.get ()
获得此int
的地址,并将其分配给ptr
。
When the scope of sp
ends, sp
will release the resource managed since there's no more *shared_ptr*s that refers to it. 当
sp
的作用域结束时,由于不再有引用它的* shared_ptr * s, sp
将释放所管理的资源。
By trying to print the value of *ptr
we are invoking undefined behavior in ( A ) since the value isn't available, it was destroyed with the destruction of sp
. 通过尝试打印
*ptr
的值,我们在( A )中调用未定义的行为 ,因为该值不可用,它被破坏了sp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.