智能指针的使用

发布于 2024-10-09 17:34:54 字数 203 浏览 8 评论 0原文

我有一个项目,我想更好地使用智能指针。

主要思想是在从函数返回新对象时使用它们。问题是使用什么智能指针?来自 boost 的 auto_ptrshared_ptr?据我所知,auto_ptr 速度较慢,但​​它可以回退到“纯”指针。

如果我在不需要的地方使用智能指针,是否会使性能变慢?

I have a project and I want make smart pointers usage better.

The main idea is to use them when returning new object from function. The question is what smart pointer to use? auto_ptr or shared_ptr from boost? As I know, auto_ptr is slower but it can fall back to the 'pure' pointer.

And if I'll use smart pointer in place where I don't need it, would it make the perfomance slower?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆夏时光 2024-10-16 17:34:54

是什么让您认为 auto_ptrshared_ptr 慢?通常,我希望相反的情况成立,因为 shared_ptr 需要更新引用计数。

至于应该使用哪个,不同的智能指针意味着不同的所有权语义。所有权意味着有责任在不再需要该对象时将其删除。

  • 原始指针意味着没有所有权;正确使用智能指针的程序可能仍然在许多不打算拥有所有权的地方使用原始指针(例如,如果您需要将对对象的可选引用传递到函数中,则通常会使用原始指针)。
  • scoped_ptr 意味着单一(即非共享)、不可转让所有权。
  • auto_ptr 意味着单一(即非共享)可转让所有权。这是我用来从函数返回新构造的对象的智能指针(该函数将对象传输给其调用者)。 auto_ptr 的缺点是,由于定义 auto_ptr 时语言的限制,很难正确使用(这给它带来了非常坏的声誉,尽管其预期目的是具有单一、可转让所有权语义的智能指针的目的过去和现在都是有效和有用的)。
  • unique_ptrauto_ptr 具有相同的语义,但使用新的 C++0x 功能(右值引用)使其比 更安全(不太容易出现错误使用) >auto_ptr。如果您在支持 unique_ptr 的平台上进行开发,那么您应该使用它而不是 auto_ptr
  • shared_ptr 意味着共享所有权。在我看来,这是过度使用的。它确实有许多有效的用途,但它不应该简单地用作默认选项。

我还要补充一点,shared_ptr 通常与 STL 容器一起使用,因为其他智能指针类在该上下文中无法实现其预期功能(由于在容器内部复制值)。这通常会导致使用 shared_ptr,其中共享所有权并不是真正的预期含义。在这些情况下,我建议(如果可能)使用 boost 指针容器类(ptr_vectorptr_map 等),它们提供了(通常需要的)a​​ 的语义。具有可转让但单一(非共享)所有权的容器。

您应该始终考虑对象的所有权:在大多数情况下,通过干净的系统设计,每个对象都有一个明显的所有者,并且所有权不需要共享。这样做的优点是可以很容易地准确地看到对象将在何时何地被释放,并且不需要引用计数开销。

[编辑以注意新的 unique_ptr]

What makes you think auto_ptr is slower than shared_ptr? Typically I would expect the reverse to be true, since shared_ptr needs to update the reference count.

As for which you should use, different smart pointers imply different ownership semantics. Ownership implies the responsibility to delete the object when it is no longer needed.

  • A raw pointer implies no ownership; a program that uses smart pointers correctly may still make use of raw pointers in a lot of places where ownership is not intended (for example, if you need to pass an optional reference to an object into a function, you would often use a raw pointer).
  • scoped_ptr implies single (ie, non-shared), non-transferable ownership.
  • auto_ptr implies single (ie, non-shared) transferable ownership. This is the smart pointer I would use to return a newly constructed object from a function (the function is transferring the object to its caller). auto_ptr suffers from the disadvantage that due to limitations of the language when auto_ptr was defined, it is difficult to use correctly (this has given it a very bad reputation, though the intended purpose of a smart pointer with single, transferable ownership semantics was and is both valid and useful).
  • unique_ptr has the same semantics as auto_ptr, but uses new C++0x features (rvalue references) to make it a lot safer (less prone to incorrect use) than auto_ptr. If you are developing on a platform where unique_ptr is available, then you should use it instead of auto_ptr.
  • shared_ptr implies shared ownership. In my opinion this is over-used. It does have many valid uses, but it should not simply be used as a default option.

I would also add that shared_ptr is often used with STL containers because the other smart pointer classes do not achieve their intended function in that context (due to copying of values internally within the container). This often leads to use of shared_ptr where shared ownership is not really the intended meaning. In these cases, I suggest (where possible) using the boost pointer-container classes (ptr_vector, ptr_map and so on), which provide the (commonly desired) semantics of a container with transferable, but single (non-shared) ownership.

You should always think about the ownership of your objects: in most cases, with a clean system design, each object has one obvious owner, and ownership does not need to be shared. This has the advantage that it is easy to see exactly where and when objects will be freed, and no reference counting overhead is needed.

[edited to note the new unique_ptr]

匿名。 2024-10-16 17:34:54

您可能应该使用shared_ptr。如果不知道自己到底想做什么,就很难说得更具体。最好阅读其文档,看看它是否符合您的要求需要。

性能差异很可能可以忽略不计。只有在极端情况下,才会产生明显的影响,例如每秒复制这些指针数百万次。

You probably should use shared_ptr<>. It's hard to be more specific without knowing what exactly you want to do. Best read its documentation and see if it does what you need.

The performance difference will most likely be negligible. Only in extreme cases there might me an noticeable impact, like when copying these pointers many million times each second.

孤独难免 2024-10-16 17:34:54

我更喜欢shared_ptr,auto_ptr会带来很多麻烦而且它的使用不太直观。如果你希望这个对象被插入到STL容器上,那么你当然想使用shared_ptr。

关于性能,您会付出一定的成本,但这很小,大多数时候您可以忽略它。

I prefer shared_ptr, auto_ptr can cause a lot of trouble and its use is not too intuitive. If you expect this object to be inserted on a STL container, so you certainly want to use shared_ptr.

Abot the performance you have a cost, but this is minimal and you can ignore it most of the time.

你是我的挚爱i 2024-10-16 17:34:54

视情况而定。
共享指针比 auto_ptr 具有更好的用途,后者具有更改分配所有权的不寻常特征。
auto_ptr 也不能在容器中使用。
另外,如果您不想转移所有权,则不能使用 auto_ptr 作为返回值。
共享指针具有智能指针的所有优点,重载了相关运算符以充当指针,并且可以在容器中使用。
话虽如此,它们的使用并不便宜。
您必须分析您的需求,以确定您是否真的通过避免共享指针实现开销而获得了一些东西

Depends.
Shared pointers have a better use than auto_ptr which have the unusual characteristic of changing ownership on assignments.
Also auto_ptr can not be used in containers.
Also you can not use auto_ptr as return values if you do not want to transfer ownership.
Shared pointers have all the benefits of smart pointers, have overloaded the relevant operators to act like a pointer and can be used in containers.
Having said that, they are not cheap to use.
You must analyze your needs to decide if you actually gain something by avoiding the shared_pointer implementation overheads

暮年 2024-10-16 17:34:54

仅使用shared_ptr。使用 auto_ptr,您只能拥有对对象的一个​​引用。而且auto_ptr并不慢,它必须比shared_ptr工作得更快。

为了不问这样的问题,你需要知道这个智能指针是如何工作的。

auto_ptr 只是存储指向对象的指针并在其析构函数中销毁它。

auto_ptr 的问题是,当您尝试复制它时,它会停止指向您的对象。

例如

auto_ptr a_ptr(new someClass);

auto_ptr another_ptr=aptr;// 之后 another_ptr 指向你的类,但 a_ptr 不再指向它了!

这就是为什么我不推荐你使用auto_ptr。

共享指针计算有多少智能指针指向您的对象,并在没有更多指针指向该对象时销毁该对象。这就是为什么你可以有超过 1 个指针指向你的对象。

但共享指针也并不完美。如果在你的程序中你有循环图(当你有类A和B并且A有一个成员shared_ptr指向B并且B有或者B的成员对象有shared_ptr指向A)那么A和B将永远不会被删除,你会有记忆舔。

要使用shared_ptr编写正确的代码,您需要小心并使用weak_ptr。
有关详细信息,请参阅此处 http://www.boost.org /doc/libs/1_45_0/libs/smart_ptr/smart_ptr.htm

Use only shared_ptr. With auto_ptr you can have ONLY ONE reference to your object. Also auto_ptr isn't slower it must work faster than shared_ptr.

To not ask such questions you need to know, how this smart pointers work.

auto_ptr just storing pointer to your object and destroying it in it's destructor.

The problem of auto_ptr is that when you are trying to copy it it's stopping to point to your object.

For example

auto_ptr a_ptr(new someClass);

auto_ptr another_ptr=aptr;// after this another_ptr is pointing to your class, but a_ptr isn't pointing to it anymore!

This is why I don't recomend you to use auto_ptr.

Shared pointer counting how much smart pointers are pointing to your object and destroying your object when there is no more pointers to it. That's why you can have more than 1 pointer pointing to your object.

But Shared pointer also isn't perfect. And if in your program you have cyclic graph (when you have classes A and B and A have a member shared_ptr wich points to B and B have or B's member objects have shared_ptr pointing to A) than A and B will never deleted and you will have memory lick.

To write correct code with shared_ptr you need to be careful and also use weak_ptr.
For more information look at here http://www.boost.org/doc/libs/1_45_0/libs/smart_ptr/smart_ptr.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文