指针,智能指针或共享指针? [英] Pointers, smart pointers or shared pointers?

查看:127
本文介绍了指针,智能指针或共享指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用正常的指针编程,但我听说过像Boost这样的实现智能指针的库。我也看到在Ogre3D渲染引擎中有一个深度使用共享指针。

I am programming with normal pointers, but I have heard about libraries like Boost that implement smart pointers. I have also seen that in Ogre3D rendering engine there is a deep use of shared pointers.

这三者之间的区别是什么,我应该坚持使用一个类型?

What exactly is the difference between the three, and should I stick on using just a type of them?

推荐答案

Sydius简要介绍了类型:

Sydius outlined the types fairly well:


  • 普通指针就是这样 - 他们指向内存中的某处。谁拥有它?只有评论会让你知道。谁释放它?

  • 智能指针是涵盖多种类型的整体术语;我假设您的意思是使用 RAII 模式的作用域指针。它是一个堆栈分配的对象,它包装一个指针;当它超出范围时,它调用对它包装的指针的delete。它拥有包含的指针,因为它负责在某点删除它。它们允许你得到一个原始的引用,它们包装的指针传递给其他方法,以及释放指针,允许别人拥有它。复制它们没有意义。

  • 共享指针是一个堆栈分配的对象,它包含指针,因此您不必知道是谁拥有它。当内存中对象的最后一个共享指针被破坏时,被包装的指针也将被删除。

  • Normal pointers are just that - they point to some thing in memory somewhere. Who owns it? Only the comments will let you know. Who frees it? Hopefully the owner at some point.
  • Smart pointers are a blanket term that cover many types; I'll assume you meant scoped pointer which uses the RAII pattern. It is a stack-allocated object that wraps a pointer; when it goes out of scope, it calls delete on the pointer it wraps. It "owns" the contained pointer in that it is in charge of deleteing it at some point. They allow you to get a raw reference to the pointer they wrap for passing to other methods, as well as releasing the pointer, allowing someone else to own it. Copying them does not make sense.
  • Shared pointers is a stack-allocated object that wraps a pointer so that you don't have to know who owns it. When the last shared pointer for an object in memory is destructed, the wrapped pointer will also be deleted.

他们?你将大量使用作用域指针或共享指针。在你的应用程序中运行了多少线程?如果答案是潜在的很多,共享指针可能会变成性能瓶颈,如果使用无处不在。原因是创建/复制/销毁共享指针需要是一个原子操作,如果你有很多线程运行,这可能会妨碍性能。但是,并不总是这样 - 只有测试会肯定会告诉你。

How about when you should use them? You will either make heavy use of scoped pointers or shared pointers. How many threads are running in your application? If the answer is "potentially a lot", shared pointers can turn out to be a performance bottleneck if used everywhere. The reason being that creating/copying/destructing a shared pointer needs to be an atomic operation, and this can hinder performance if you have many threads running. However, it won't always be the case - only testing will tell you for sure.

有一个参数(我喜欢)对共享指针 - 使用它们,你允许程序员忽略谁拥有指针。这可能导致循环引用(Java将检测这些,但共享指针不能)或一般程序员在大代码库中懒惰的棘手情况。

There is an argument (that I like) against shared pointers - by using them, you are allowing programmers to ignore who owns a pointer. This can lead to tricky situations with circular references (Java will detect these, but shared pointers cannot) or general programmer laziness in a large code base.

有两个原因使用作用域指针。第一个是简单的异常安全和清除操作 - 如果你想保证一个对象被清理,不管面对异常,并且你不想堆栈分配该对象,把它放在一个作用域指针。如果操作成功,您可以随意将它转移到一个共享指针,但同时用一个作用域指针保存开销。

There are two reasons to use scoped pointers. The first is for simple exception safety and cleanup operations - if you want to guarantee that an object is cleaned up no matter what in the face of exceptions, and you don't want to stack allocate that object, put it in a scoped pointer. If the operation is a success, you can feel free to transfer it over to a shared pointer, but in the meantime save the overhead with a scoped pointer.

其他情况是当你想要清除对象所有权。有些团队喜欢这个,有些不喜欢。例如,数据结构可以返回指向内部对象的指针。在作用域指针下,它将返回一个原始指针或引用,应该被视为弱引用 - 在拥有它的数据结构被破坏后访问该指针是一个错误,删除它是一个错误。在共享指针下,拥有对象不能破坏它返回的内部数据,如果有人仍然持有一个句柄 - 这可能会导致资源打开的时间比必要的更长,或者更糟的取决于代码。

The other case is when you want clear object ownership. Some teams prefer this, some do not. For instance, a data structure may return pointers to internal objects. Under a scoped pointer, it would return a raw pointer or reference that should be treated as a weak reference - it is an error to access that pointer after the data structure that owns it is destructed, and it is an error to delete it. Under a shared pointer, the owning object can't destruct the internal data it returned if someone still holds a handle on it - this could leave resources open for much longer than necessary, or much worse depending on the code.

这篇关于指针,智能指针或共享指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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