Smart Pointer Guidelines
Smart Pointer Guidelines
What are smart pointers?Smart pointers are a specific kind of "scoping object". They are like regular pointers but can automatically deallocate the object they point to when they go out of scope. Since C++ is not a garbage collected language, such functionality is important. The pattern where scoping objects are used to automatically manage the lifetime of heap-allocated objects is called RAII - Resource Acquisition Is Initialization. Here's some sample use of // We can put a pointer into a std::unique_ptr<> at construction time... Why do we use them?Smart pointers ensure we properly destroy an object even if its creation and destruction are widely separated. They make functions simpler and safer by ensuring that no matter how many different exit paths exist, local objects are always cleaned up correctly. They help enforce that exactly one object owns another object at any given time, preventing both leaks and double-frees. Finally, their use clarifies ownership transference expectations at function calls. What types of smart pointers exist?The two common smart pointers in Chromium are base/memory/ has a few other objects of interest:
When do we use each smart pointer?
What are the calling conventions involving different kinds of pointers?See the calling conventions section of the Chromium style guide for the rules; some common cases are illustrated below.
What about passing or returning a smart pointer by reference?Don't do this. In principle, passing a One exception is lambda functions used with STL algorithms operating on containers of smart pointers; these may have to take e.g. I want to use an STL container to hold pointers. Can I use smart pointers?Yes! As of C++11, you can store smart pointers in STL containers. In particular, there's no longer a need to use General references on smart pointers |
Smart Pointer Guidelines的更多相关文章
- [CareerCup] 13.8 Smart Pointer 智能指针
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...
- 理解smart pointer之三:unique_ptr
unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一. std::unique_ptr is a smart pointer that retain ...
- Why do we need smart pointer and how to implement it.
Here are two simple questions. Problem A #include <string> include <iostream> using name ...
- c++ smart pointer
智能指针(smart pointer)是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动态分配的对象,防止内存泄露.它的一种通用实现技术是使用引用计数(reference ...
- c++(smart pointer)
(一)首先对智能指针有一些概念性的了解 **********本部分内容摘自开源中国社区http://my.oschina.net/u/158589/blog/28994******** 1.什么是智能 ...
- c/c++ 标准库 智能指针( smart pointer ) 是啥玩意儿
标准库 智能指针( smart pointer ) 是啥玩意儿 一,为什么有智能指针??? c++程序员需要自己善后自己动态开辟的内存,一旦忘了释放,内存就泄露. 智能指针可以帮助程序员"自 ...
- C++ smart pointer智能指针
在C++中,程序员可以直接操作内存,给编程增加了不少的灵活性.但是灵活性是有代价的,程序员必须负责自己负责释放自己申请的内存,否则就会出现内存泄露.智能指针就是为了解决这个问题而存在的.它和其他指 ...
- Effective C++ Item 17 Store newed objects in smart pointer in standalone statements
If you trying to do multiple things in one statement, you should think carefully abnormal behavior e ...
- Smart pointer 智能指针小总结
Smart pointer line 58之后smart pointer里的计数已经是0,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如 ...
随机推荐
- 05004_Linux的其他命令和权限命令
1.其他命令 (1)显示当前所在位置 命令:pwd (2)搜索命令 a.命令:grep 要搜索的字符串 要搜索的文件 示例:搜索/etc/sudu.conf文件中包含字符串to的行 b.搜索/etc/ ...
- 转:强制Visual Studio以管理员身份运行
Windows 8的一个既安全又蛋疼之处是UAC的行为被改变了.以往在Windows 7中,只要关闭了UAC,自己的帐号又是本机管理员组的,任何程序都会以管理员身份启动.然而,在Windows 8上, ...
- UVA 11020 Efficient Solutions+multiset的应用
题目链接:点击进入 首先来讲,非常easy看到我们事实上仅仅要维护优势人群的集合:假设增加一个新的人,我们首先看一下优势人群中是否有人会让这个人失去优势,假设没有,则将这个人插入集合中.但要注意到这个 ...
- nyoj33 蛇形填数
蛇形填数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.比如n=4时方陈为: 10 11 12 1 9 16 ...
- CLion注冊码算法逆向分析实录(纯研究)
声明 CLion程序版权为jetBrains全部.注冊码授权为jetBrains及其付费用户全部,本篇仅仅从兴趣出发,研究其注冊码生成算法. 不会释出不论什么完整的源码. 网上查了下.已有注冊机,所以 ...
- Android设置头像,手机拍照或从本地相冊选取图片作为头像
[Android设置头像,手机拍照或从本地相冊选取图片作为头像] 像微信.QQ.微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1,让用户通过选择本地相冊之类的图片库中已 ...
- HDOJ 5087 Revenge of LIS II DP
DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- 【每日一记】unity3d 图片置灰shader
项目须要.參考了一下网上的资料,写了一个shader,给button置灰. shader详细实现例如以下: 2016.07.25补充,支持mask剪裁 Shader "Transparent ...
- What's the difference between returning void and returning a Task?
http://stackoverflow.com/questions/8043296/whats-the-difference-between-returning-void-and-returning ...
- java9新特性-21-java的动态编译器
1. 官方Feature 243: Java-Level JVM Compiler Interface 295: Ahead-of-Time Compilation 2. 产生背景 Oracle 一直 ...