简体   繁体   English

我应该在所有内容上使用智能指针而忘记经典的普通指针吗?

[英]Should I use smart pointers on everything and forget about classic normal pointers?

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. 我一直在使用C ++并且非常了解分配和释放内存时的谨慎,特别是不要忘记删除未使用的实例。

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. 现在,我刚刚使用了boost并遇到了一个我面临的问题,我被迫使用智能指针(特别是shared_ptr)。 So, if I am going to use shared_ptr for this problem, should I use smart pointers to all my normal pointer codebase? 所以,如果我要使用shared_ptr来解决这个问题,我应该使用智能指针来解决所有正常的指针代码库吗?

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. 如有必要,使用智能指针补充,但不要忘记所有权。

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. 但这并不意味着你应该在大多数情况下使用::boost::shared_ptr In fact I think you should use shared_ptr sparingly and carefully. 事实上,我认为你应该谨慎小心地使用shared_ptr

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 . 但对于那些你不使用shared_ptr指针,你应该使用::std::auto_ptr或者,如果你有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. 指向在C库中分配的东西可能需要一个自定义的智能指针,该指针称为free而不是delete。 Maybe you want a stretchy buffer you can call realloc on and don't want to use a ::std::vector for some reason. 也许你想要一个可以调用realloc的弹性缓冲区,并且由于某些原因不想使用::std::vector

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. 但通常,智能指针(虽然通常不是::boost::shared_ptr (或在C ++ 0x ::std::shared_ptr ))是您的资源管理问题的正确答案。

Yes. 是。 You should be using the Boost smart pointers for most everything if you have them available. 如果你有可用的话,你应该使用Boost智能指针来处理大多数事情。 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. 我还建议在你的类中使用scoped_ptr,即使你的类设计得很好。 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. 你想要的最后一件事是必须追踪内存问题,因为有人忘记将指针初始化为NULL并且它正在传递if语句检查。

No, you should not use smart pointers for everything. 不,你不应该使用智能指针。 What you should consider whenever typing new : 输入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!" “我不想打算弄清楚哪个对象拥有这个指针以及它的生命周期应该是什么因此我会把它变成一个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. ,答案可以是一个智能指针,但通常情况下,它不需要。

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, ie A pointing to B and B pointing to A, or even something pointing to itself. 如果你有引用循环,智能指针将无法正常工作,即A指向B和B指向A,甚至指向自身的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM