博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[SOF] Pointers, smart pointers or shared pointers?
阅读量:5307 次
发布时间:2019-06-14

本文共 4443 字,大约阅读时间需要 14 分钟。

 

My list of pointers:

  • normal usage: normal members and (const) references to them
  • sharing and keeping the object alive (owners, containers): shared_ptr
  • sharing, but not keeping alive (users): weak_ptr
  • scoped usage: scoped_ptr
  • other usages (output-parameter,...): raw pointer

link 

Sydius outlined the types fairly well:

  • 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  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.

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.

~~~~~~~~~ 

the term "smart pointer" includes shared pointers, auto pointers, locking pointers and others. you meant to say auto pointer (more ambiguously known as "owning pointer"), not smart pointer.

Dumb pointers (T*) are never the best solution. They make you do explicit memory management, which is verbose, error prone, and sometimes nigh impossible. But more importantly, they don't signal your intent.

Auto pointers delete the pointee at destruction. For arrays, prefer encapsulations like vector and deque. For other objects, there's very rarely a need to store them on the heap - just use locals and object composition. Still the need for auto pointers arises with functions that return heap pointers -- such as factories and polymorphic returns.

Shared pointers delete the pointee when the last shared pointer to it is destroyed. This is useful when you want a no-brainer, open-ended storage scheme where expected lifetime and ownership can vary widely depending on the situation. Due to the need to keep an (atomic) counter, they're a bit slower than auto pointers. Some say half in jest that shared pointers are for people who can't design systems -- judge for yourself.

For an essential counterpart to shared pointers, look up weak pointers too. 

转载于:https://www.cnblogs.com/chenrui/archive/2012/10/23/2735632.html

你可能感兴趣的文章
安卓ROOT工具汇总
查看>>
大臣的旅费
查看>>
20180101
查看>>
查看SQL SERVER Job details
查看>>
ts关键还是js 因为要编译成js
查看>>
南阳56
查看>>
基于android studio的快捷开发(将持续更新)
查看>>
《构建之法》阅读笔记03
查看>>
理解多线程中的ManualResetEvent(C#)
查看>>
.Net实现微信公众平台开发接口(二) 之 “获取access_token”
查看>>
Detecting iOS
查看>>
AJAXPro用法
查看>>
[ Git ] [ GitHub ] 如何刪除自己github上面的Repository-轉載
查看>>
微信小程序<每日查看>开发总结
查看>>
asp.net XML转JSON
查看>>
Java 编码规范
查看>>
Uva11300/BZOJ1045/BZOJ1465/BZOJ3292
查看>>
Android访问网络系列之--服务端返回XML或JSON格式数据,Android 进行解析并显示
查看>>
git远程分支创建和推送【转载】
查看>>
python3爬虫之开篇
查看>>