C++ 中的智能指针入门

发布于 2024-09-26 07:44:59 字数 810 浏览 3 评论 0原文

我有一个 C++ 应用程序,它广泛使用指针来维护相当复杂的数据结构。该应用程序对巨大的数据集(可能需要几 GB 内存)执行数学模拟,并使用 Microsoft 的 Visual Studio 2010 进行编译。

我现在正在重新设计该应用程序的重要部分。为了减少错误(悬空指针、内存泄漏……),我想开始使用智能指针。只要有限,牺牲内存或性能是可以接受的。

实际上,大多数类都维护在大池中(每个类一个池),尽管这些类可以相互引用,但您可以将池视为该类的所有实例的所有者。但是,如果池决定删除一个实例,我不希望任何仍然引用已删除实例的其他类具有悬空指针。

在另一部分中,我保留了指向应用程序中其他模块提供的实例的指针的集合。在实践中,其他模块维护所传递实例的所有权,但在某些情况下,模块不想处理所有权,而只想将实例传递给集合,告诉它“现在是你的了,管理它”。

开始引入智能指针的最佳方式是什么?仅用智能指针[随机]替换指针似乎不是正确的方法,并且可能无法提供智能指针的所有(或任何)优点。但更好的方法是什么?

我应该进一步研究哪些类型的智能指针?我有时使用 std::auto_ptr 来释放本地分配的内存,但这在 C++0x 中似乎已被弃用。 std::unique_ptr 是更好的选择吗?或者我应该直接使用共享指针或其他类型的智能指针?

问题用智能指针替换现有的原始指针似乎类似,但不是问它有多容易,我问最好的方法是什么,以及哪种智能指针最适合。

预先感谢您的想法和建议。

I have a C++ application which makes extensively use of pointers to maintain quite complex data structures. The application performs mathematical simulations on huge data sets (which could take several GB of memory), and is compiled using Microsoft's Visual Studio 2010.

I am now reworking an important part of the application. To reduce errors (dangling pointers, memory leaks, ...) I would want to start using smart pointers. Sacrificing memory or performance is acceptible as long as it is limited.

In practice most of the classes are maintained in big pools (one pool per class) and although the classes can refer to each other, you could consider the pool as owner of all the instances of that class. However, if the pool decides to delete an instance, I don't want any of the other classes that still refers to the deleted instance to have a dangling pointer.

In another part I keep a collection of pointers to instances that are delivered by other modules in the application. In practice the other modules maintain ownership of the passed instance, but in some cases, modules don't want to take care of the ownership and just want to pass the instance to the collection, telling it "it's yours now, manage it".

What is the best way to start introducing smart pointers? Just replacing pointers [at random] with smart pointers doesn't seem a correct way, and probably doesn't deliver all the (or any of the) advantages of smart pointers. But what is a better method?

Which types of smart pointers should I further investigate? I sometimes use std::auto_ptr for the deallocation of locally allocated memory, but this seems to be deprected in C++0x. Is std::unique_ptr a better alternative? Or should I go straight to shared pointers or other types of smart pointers?

The question Replacing existing raw pointers with smart pointers seems similar but instead of asking how easy it is, I am asking what the best approach would be, and which kind of smart pointers are suited best.

Thanks in advance for your ideas and suggestions.

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

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

发布评论

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

评论(2

转角预定愛 2024-10-03 07:44:59

我建议尽可能使用unique_ptr(这可能需要一些程序分析),而当不可能时使用shared_ptr。如有疑问,请使用 shared_ptr 最大限度地提高安全性:将控制权移交给容器时,引用计数将简单地变为 2,然后返回到 1,容器最终将删除 code> 自动关联对象。当性能成为问题时,请考虑使用 boost::intrusive_ptr

I recommend using unique_ptr when possible (this may require some program analysis) and shared_ptr when this is impossible. When in doubt, use a shared_ptr to maximize safety: when handing off control to a container, the reference count will simply go to two and then back to one and the container will eventually delete the associated object automatically. When performance becomes an issue, consider using boost::intrusive_ptr.

§对你不离不弃 2024-10-03 07:44:59

以下是新 C++11 标准中的 3 个变体(unique_ptr 取代 auto_ptr)

http://www.stroustrup.com/C++11FAQ.html#std-unique_ptr

http://www.stroustrup.com/C++11FAQ.html#std-shared_ptr

http://www.stroustrup.com/C++11FAQ.html#std-weak_ptr

您可以阅读每个指针的文本,并有一个解释何时使用其中的哪个。对于本地内存管理,可以选择 unique_ptr。它是不可复制的,但可以移动,因此当您在周围移动它时,接收者就获得了它的所有权。

如果您想在没有人真正拥有该对象的情况下共享对象实例,并确保在有人仍然拥有对该对象的引用时该对象不会被删除,则可以使用 Shared_ptr。一旦对象的最后一个用户销毁了shared_ptr容器,所包含的对象将被删除。

weak_ptr 与shared_ptr 结合使用。它使人们能够在尝试访问内部对象之前“锁定”以查看引用的shared_ptr对象是否仍然存在。

Here are the 3 varieties found in the new C++11 standard (unique_ptr replaces auto_ptr)

http://www.stroustrup.com/C++11FAQ.html#std-unique_ptr

http://www.stroustrup.com/C++11FAQ.html#std-shared_ptr

http://www.stroustrup.com/C++11FAQ.html#std-weak_ptr

You can read the text for each pointer and there is an explanation of when to use which in there. For local memory management unique_ptr is the choice. It is non-copyable but movable so as you move it around the receiver takes ownership of it.

Shared_ptr is used if you want to share an object instance around with no one really owning the object and to make sure it doesn't get deleted while someone still has a reference to it. Once the last user of an object destroys the shared_ptr container, the contained object will be deleted.

weak_ptr is used in conjunction with shared_ptr. It enables one to "lock" to see if the reference shared_ptr object still exists before trying to access the internal object.

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