auto_ptr& amp;是什么意思?的unique_ptr? [英] what's the point of auto_ptr & unique_ptr?

查看:67
本文介绍了auto_ptr& amp;是什么意思?的unique_ptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题已移至此处: http: //www.codeproject.com/Messages/4806617/whats-the-point-of-auto-ptr-unique-ptr.aspx [ ^ ]

the queston had been moved here: http://www.codeproject.com/Messages/4806617/whats-the-point-of-auto-ptr-unique-ptr.aspx[^]

推荐答案

你可能正确理解 auto_ptr ,但是你的逻辑背叛了你。好吧,背叛......非常糟糕。是的,你说的往往更好,那又怎样?它是否会使自动堆释放一般无意义?问题应该是正确的:它不是关于什么是更好,堆或堆栈,这是更具体的事情:什么是更好的堆使用堆?



如果你能证明不应该使用堆,你可以证明你的观点。如果你不能这么说,你甚至不应该建议你在问题中建议的替代方案,因为,一旦使用堆,解除分配是一个问题。



如果你想要声明不需要堆(我想假设,只是一秒钟),这意味着你不理解堆栈是1)更有限的资源,2)它不能支持更复杂的所需的免费分配结构(这很简单;例子是链表和树,留出一般结构图)。



另外,请注意 auto_ptr 在C ++ 11中被弃用,被更灵活和更强大的 unique_ptr 取代:

http://www.cplusplus.com/reference/memory/auto_ptr [ ^ ],

http://en.cppreference.com/w/cpp/memory/unique_ptr [ ^ ]。



-SA
Probably, you understand auto_ptr correctly, but your logic betrays you. Well, betrays… very badly. Yes, what you say is often better, so what? Does it make the automatic heap deallocation pointless in general? The problem should be posed correctly: it's not about what is better, heap or stack, this is about more particular thing: what is better when the use of the heap is already given?

You could possibly prove your point if you could prove that heap should not be used at all. If you cannot say so, you should not even suggest the alternative you suggested in your question, because, once heap is used, deallocation is a problem.

And if you wanted to state that heap is not needed (I want to assume, just for a second), it would mean that you did not understand that stack is 1) much more limited resource, 2) it cannot support free allocation needed for most more sophisticated structures (which are simple enough; the examples are the linked list and the tree, set aside general-structure graphs).

Also, note that auto_ptr is deprecated in C++11, superseded by more flexible and powerful unique_ptr:
http://www.cplusplus.com/reference/memory/auto_ptr[^],
http://en.cppreference.com/w/cpp/memory/unique_ptr[^].

—SA


这篇关于auto_ptr& amp;是什么意思?的unique_ptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆