我应该在所有事情上使用智能指针并忘记经典的普通指针吗?
我已经使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该小心使用智能指针。在考虑内存管理时,它们并不是灵丹妙药。循环引用仍然是一个问题。
在进行类设计时,始终要考虑谁拥有某个对象的所有权(有责任销毁该对象)。如有必要,用智能指针对其进行补充,但不要忘记所有权。
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.
是的,对于几乎所有事情,你应该更喜欢智能指针而不是裸指针。但这并不意味着您应该在大多数情况下使用
::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 useshared_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 callrealloc
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.是的。如果您有可用的 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.
不,你不应该对所有事情都使用智能指针。
每当输入
new
时您应该考虑什么:有时,解决方案是智能指针,但也是懒惰的答案。 “我不想费心去弄清楚哪个对象拥有这个指针以及它的生命周期应该是多少,因此我将把它设为一个共享指针!”
重要的问题是; 什么在管理这个对象的生命周期?,答案可以是智能指针,但通常情况下,并不需要如此。
No, you should not use smart pointers for everything.
What you should consider whenever typing
new
: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.
不,这取决于你在做什么。
No. It depends on what you're doing.