我应该在所有事情上使用智能指针并忘记经典的普通指针吗?

发布于 2024-10-04 01:47:40 字数 170 浏览 5 评论 0原文

我已经使用 C++ 很长时间了,非常清楚分配和释放内存时要小心,尤其是不要忘记删除未使用的实例。

现在,我最近刚刚使用了 boost,并且遇到了一个问题,我被迫使用智能指针(特别是 shared_ptr)。那么,如果我要使用shared_ptr来解决这个问题,我应该使用智能指针来指向我的所有普通指针代码库吗?

I've been using C++ for a long time and know very well about carefulness in allocating and deallocating memory, especially not forgetting to delete unused instances.

Now, I've just recently used boost and with a problem I am facing I'm forced to used smart pointers (specifically the shared_ptr) one. So, if I am going to use shared_ptr for this problem, should I use smart pointers to all my normal pointer codebase?

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

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

发布评论

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

评论(5

℡寂寞咖啡 2024-10-11 01:47:40

您应该小心使用智能指针。在考虑内存管理时,它们并不是灵丹妙药。循环引用仍然是一个问题。

在进行类设计时,始终要考虑谁拥有某个对象的所有权(有责任销毁该对象)。如有必要,用智能指针对其进行补充,但不要忘记所有权。

You should use smart pointers careful. They are not the silver bullet when considering memory management. Circular references are still an issue.

When making the class design, always think who has the ownership of an object (has the responsibility to destroy that object). Complement that with smart pointers, if necessary, but don't forget about the ownership.

调妓 2024-10-11 01:47:40

是的,对于几乎所有事情,你应该更喜欢智能指针而不是裸指针。但这并不意味着您应该在大多数情况下使用 ::boost::shared_ptr 。事实上,我认为您应该谨慎谨慎地使用 shared_ptr

但对于这些指针,您不使用 shared_ptr 因为您应该使用 ::std::auto_ptr 或者,如果您有 C++0x ::std ::unique_ptr。如果它们不合适,您应该找到合适的智能指针类型。

现在,这并不总是正确的答案,只是几乎总是正确的答案。即使面对异常或其他此类奇怪的控制流,智能指针对于适当地跟踪和释放内存资源也是非常宝贵的。

在某些情况下,您需要编写自己的智能指针类或不使用它。这些案例非常罕见,但确实存在。

例如,在您自己的智能指针类中使用智能指针可能不是正确的做法。指向 C 库中分配的内容可能需要一个名为“free”而不是“delete”的自定义智能指针。也许您想要一个可以调用 realloc 的弹性缓冲区,并且出于某种原因不想使用 ::std::vector

但一般来说,智能指针(尽管通常不是 ::boost::shared_ptr (或在 C++0x ::std::shared_ptr 中))是正确的答案你的资源管理问题。

Yes, you should greatly prefer smart pointers over bare pointers for almost everything. But that does not mean you should be using ::boost::shared_ptr for most of those cases. In fact I think you should use shared_ptr sparingly and carefully.

But for those pointers you don't use shared_ptr for you should be using ::std::auto_ptr or, if you have C++0x ::std::unique_ptr. And if they aren't appropriate, you should find a smart pointer type that is.

Now this isn't always the right answer, just almost always. Smart pointers are invaluable for tracking and freeing memory resources appropriately even in the face of exceptions or other such oddities of control flow.

There are cases in which you will need to either write your own smart pointer class or not use one. These cases are very rare, but they exist.

For example, using a smart pointer in your own smart pointer class is probably not the right thing to be doing. Pointing at stuff allocated in a C library would probable require a custom smart pointer that called free instead of delete. Maybe you want a stretchy buffer you can call realloc on and don't want to use a ::std::vector for some reason.

But generally, a smart pointer (though not usually ::boost::shared_ptr (or in C++0x ::std::shared_ptr)) is the right answer to your resource management problem.

一曲爱恨情仇 2024-10-11 01:47:40

是的。如果您有可用的 Boost 智能指针,您应该在大多数情况下使用它们。请记住这一点,虽然您可以并且从您的评论中确实有效地使用了指针,但在您之后的人可能不会。使用智能指针可以并且将会(希望不能防止不良代码)缓解其中一些问题。

我还建议在您的课程中使用 scoped_ptr如果你的课程设计得好。它将有助于防止下一个人在面对裸指针时可能/可能/最有可能引入的问题。通过举例,它也会鼓励他们使用它们。您最不想看到的就是必须追踪内存问题,因为有人忘记将指针初始化为 NULL 并且它通过了 if 语句检查。

Yes. You should be using the Boost smart pointers for most everything if you have them available. Remember this, while you can and from your comments do use pointers effectively, the person/people that come after you may not. Using the smart pointers can and will (hopefully, can't guard against bad code) mitigate some of these issues.

I'd also recommend using scoped_ptr inside your classes even if your classes are designed well. It'll help guard against issues the next guy could/might/most likely will introduce when faced with a naked pointer. It'll encourage them to use them too, by example. The last thing you want is to have to track down a memory issue because someone forgot to initialize the pointer to NULL and it's passing the if statement check.

凉城 2024-10-11 01:47:40

不,你不应该对所有事情都使用智能指针。
每当输入 new 时您应该考虑什么:

  • 该对象的生命周期是多长?
  • 哪个对象拥有它?
  • 该对象将如何管理其生命周期?

有时,解决方案是智能指针,但也是懒惰的答案。 “我不想费心去弄清楚哪个对象拥有这个指针以及它的生命周期应该是多少,因此我将把它设为一个共享指针!”

重要的问题是; 什么在管理这个对象的生命周期?,答案可以是智能指针,但通常情况下,并不需要如此。

No, you should not use smart pointers for everything.
What you should consider whenever typing new:

  • What is the lifetime of this object?
  • Which object owns it?
  • How is that object going to manage its lifetime?

Sometimes the solution is a smart pointer but also the lazy answer. "I don't want to be bothered to work out which object owns this pointer and what its lifetime should be hence I'll make it a shared_pointer!"

The important question is; What is managing the lifetime of this object?, the answer can be a smart pointer, but oftentimes, it doesn't need to be.

傲鸠 2024-10-11 01:47:40

不,这取决于你在做什么。

  • 智能指针有性能开销。在桌面应用程序中,这通常不是一个问题,但根据您的操作,它可能会是一个问题。
  • 如果有引用循环,即 A 指向 B,B 指向 A,甚至某个东西指向自身,智能指针将无法正常工作。

No. It depends on what you're doing.

  • Smart pointers have a performance overhead. In desktop applications, this is typically not a concern, but depending on what you do, it might be.
  • Smart pointers will not work right if you have reference cycles, i.e. A pointing to B and B pointing to A, or even something pointing to itself.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文