共享指针与作用域指针
scoped_ptr
不可复制,正在从作用域中删除。所以它是一种受限制的shared_ptr
。因此,除了确实需要限制复制操作的情况之外,似乎使用 shared_ptr
更好。因为有时您不知道是否需要创建对象的副本。所以问题是:除了上面提到的情况之外,我们是否可以认为使用shared_ptr
比scoped_ptr
更好(或推荐)使用。 scoped_ptr
是否比 shared_ptr
工作得更快,或者它有什么优势吗?
谢谢!
scoped_ptr
is not copy able and is being deleted out of the scope. So it is kind of restricted shared_ptr
. So seems besides the cases when you really need to restrict the copy operation shared_ptr
is better to use. Because sometimes you don’t know you need to create a copy of your object or no. So the question is: besides the cases mentioned above, could we consider that shared_ptr
is better (or recommended) to use instead of scoped_ptr
. Does scoped_ptr
work much faster from shared_ptr
, or does it have any advantages?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
shared_ptr
比scoped_ptr
更重量级。它需要分配和释放引用计数对象以及托管对象,并处理线程安全的引用计数——在我工作的一个平台上,这是一项巨大的开销。我的建议(一般来说)是使用满足您需求的最简单的对象。如果需要引用计数共享,请使用
shared_ptr
;如果您只需要在完成单个引用后自动删除,请使用scoped_ptr
。shared_ptr
is more heavyweight thanscoped_ptr
. It needs to allocate and free a reference count object as well as the managed object, and to handle thread-safe reference counting - on one platform I worked on, this was a significant overhead.My advice (in general) is to use the simplest object that meets your needs. If you need reference-counted sharing, use
shared_ptr
; if you just need automatic deletion once you've finished with a single reference, usescoped_ptr
.性能 -
shared_ptr
具有更多功能,但也需要额外的分配(它也更大,但这很少重要)。[edit] 使用
make_shared
可以避免第二次分配,但是即使在对象被销毁之后weak_ptr
仍将保留整个分配,这对于大型设备来说可能是一个问题对象。意图表达 使用
scoped_ptr
您可以更明确地陈述您想要做什么。 (如果你想知道 - 这是一件好事:))。如果您正确执行此操作,shared_ptr 还将指示“此对象旨在超出此范围”Performance -
shared_ptr
has more functionality, but also requires an additional allocation (it's also larger, but that rarely matters).[edit] The second allocation can be avoided by using
make_shared
, but thenweak_ptr
will hold the entire entire allocation even after the object is destroyed, which may be a problem for large objects.Expresison of Intent using
scoped_ptr
you state more explicitly what you want to do. (In case you wonder - that's a good thing :) ). If you do this correctly, shared_ptr will also indicate "this object is intended to live beyond this scope"它们的预期目的不同,因此,在许多情况下,“shared_ptr 与scoped_ptr”根本不是问题。当然,当您只需要一个scoped_ptr 时,您可以使用shared_ptr。但有什么意义呢?由于涉及所有引用计数,shared_ptr 的开销可能会稍大一些。
Their intended purpose is different, so, in many cases "shared_ptr vs scoped_ptr" is not a question at all. Sure, you can use a shared_ptr when all you need is a scoped_ptr. But what's the point? shared_ptr has likely a slightly bigger overhead with all the reference counting involved.
scoped_ptr
的工作速度比shared_ptr
快得多。是的。shared_ptr
始终使用您的分配器或默认分配器分配内存。scoped_ptr
works much faster fromshared_ptr
. It's right.shared_ptr
always allocate memory using your allocator or default allocator.Scoped_ptr 与shared_ptr、weak_ptr 或unique_ptr 没有什么共同点,因为它只执行非常特殊的“引用计数”情况。在精心设计的代码中,您不会经常需要它,但它是一个很好的工具。
基本上,scoped_ptr 根本不是引用计数的东西。相反,它是您在堆栈上(在本地范围内)创建的对象,以便您可以执行以下操作:
您倾向于更多地使用互斥体和其他同步原语看到这种模式 - 我可以声明一个将锁定的“AutoLock”互斥锁传递给它,然后在删除时解锁它,将整个“{}”范围变成关键部分。
另请注意,只有当您出于某种原因无法执行像“MyObject obj(params..)”这样的普通堆栈分配时,“scoped_ptr”才有意义。毕竟,它所做的就是让您使用堆分配的对象,就好像它是堆栈上的对象一样。这往往比shared_ptr & 的引用计数要少见得多。它的表兄弟。
Scoped_ptr has little in common with shared_ptr, weak_ptr, or unique_ptr because it is only doing very special case of "reference counting". It isn't something you will need very often in well-designed code, but it is a good tool to have available.
Basically, a scoped_ptr isn't a reference-counted thing at all. Rather, it is an object you create on the stack (within the local scope) so that you can do something like this:
You tend to see this pattern more with mutexes and other synchronization primatives- I can declare an "AutoLock" that will lock the mutex passed into it, then unlock it when it deletes to turn the whole "{}" scope into a critical section.
Also notice that a 'scoped_ptr' only ever makes sense when you can't just do a plain-old stack allocation like "MyObject obj(params..)" for some reason. After all, what it is doing is letting you use a heap-allocated object as if it was one on the stack. That tends to be a lot rarer a use case than the reference-counting of shared_ptr & its cousins.