C++ 中自动指针和共享指针有什么区别?
我听说自动指针拥有它们的对象,而共享指针可以有许多对象指向它们。为什么我们不一直使用共享指针呢?
与此相关的是,什么是智能指针,人们可以将这个术语与共享指针互换使用。它们是一样的吗?
I have heard that auto pointers own their object whereas shared pointers can have many objects pointing to them. Why dont we use shared pointers all the time.
In relation to this what are smart pointers, people use this term interchangeably with shared pointers. Are they the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
std::auto_ptr
是独占指针所有权的过时且已弃用的实现。在 C++11 中它已被std::unique_ptr
取代。独占所有权意味着指针由某物拥有,并且该对象的生命周期与所有者的生命周期相关联。共享指针 (
std::shared_ptr
) 实现共享指针所有权 - 只要存在对对象的活动引用,它们就会使对象保持活动状态,因为没有单一所有者。它通常通过引用计数来完成,这意味着与唯一指针相比,它们具有额外的运行时开销。此外,推理共享所有权比推理独占所有权更困难——破坏点变得不那么确定。智能指针这个术语涵盖了所有行为类似于指针的类型,但添加了(智能)语义,而不是原始的
T*
。unique_ptr
和shared_ptr
都是智能指针。std::auto_ptr
is an outdated, deprecated implementation of exclusive pointer ownership. It's been replaced bystd::unique_ptr
in C++11. Exclusive ownership means that the pointer is owned by something, and the lifetime of the object is tied to the lifetime of the owner.Shared pointers (
std::shared_ptr
) implement shared pointer ownership — they keep the object alive as long as there are alive references to it, because there is no single owner. It's usually done with reference counting, which means they have additional runtime overhead as opposed to unique pointers. Also reasoning about shared ownership is more difficult than reasoning about exclusive ownership — the point of destruction becomes less deterministic.Smart pointer is a term that encompasses all types that behave like pointers, but with added (smart) semantics, as opposed to raw
T*
. Bothunique_ptr
andshared_ptr
are smart pointers.共享指针的成本稍高,因为它们保存引用计数。
在某些情况下,如果您有一个在多个递归级别上具有共享指针的复杂结构,则一次更改可能会影响其中许多指针的引用计数。
同样在多 CPU 核心架构中,如果多个核心当前正在访问同一内存区域,则引用计数的原子更新可能至少会变得不那么昂贵,但实际上确实非常昂贵。
然而,共享指针使用起来简单且安全,而自动指针的赋值属性则令人困惑,并且可能变得非常危险。
智能指针通常被频繁用作共享指针的同义词,但实际上涵盖了 boost 中的所有各种指针实现,包括与共享指针类似的指针。
Shared pointers are slightly more costly as they hold a reference count.
In some case, if you have a complex structure with shared pointer at multiple recursive levels, one change can touch the reference count of many of those pointers.
Also in multiple CPU core architectures, the atomic update of a reference count might become not slightly costly at least, but actually really costly, if the multiple core are currently accessing the same memory area.
However shared pointers are simple and safe to use, whereas the assignment properties of auto pointers is confusing, and can become really dangerous.
Smart pointers usually is frequently used just as a synonym of shared pointer, but actually covers all the various pointers implementation in boost, including the one that's similar to shared pointers.
智能指针可以有多种形式。现在 C++11 中受 Boost 启发的 Shared_ptr 就是其中之一。我建议在有疑问时在几乎所有地方使用shared_ptr,而不是使用auto_ptr,后者有很多怪癖。
简而言之,shared_ptr 只是共享同一对象的引用计数实现。
参考:
http://www.gotw.ca/publications/using_auto_ptr_ effectively.htm
http://en.cppreference.com/w/cpp/memory/shared_ptr
There can be many forms of smart pointers. Boost inspired shared_ptr which is now in C++11 is one of them. I suggest using shared_ptr at almost all the places when in doubt instead of auto_ptr which has many quirks.
In short, shared_ptr is just a reference counting implementation to share same object.
Refer:
http://www.gotw.ca/publications/using_auto_ptr_effectively.htm
http://en.cppreference.com/w/cpp/memory/shared_ptr