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,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如 ...
随机推荐
- ping,telnet,ssh命令的理解
ping主要用于测试到达目的主机的网络是否连接,但是它不能检测某个端口是否开放. ping 域名可以直接看出这个域名对应的ip ssh与telnet都是远程登录工具. ssh对传输加密,安全性高,te ...
- FS,FT,DFT,DFS和DTFT的关系
对于初学数字信号(Digital Signal Processing,DSP)的人来说,这几种变换是最为头疼的,它们是数字信号处理的理论基础,贯穿整个信号的处理. FS:时域上任意连续的周期信号可以分 ...
- Qt之QStackedLayout
简述 QStackedLayout继承自QLayout. QStackedLayout类提供了多页面切换的布局,一次只能看到一个界面. QStackedLayout可用于创建类似于QTabWidget ...
- 基于json数据格式实现的简单数据库——jsonDB
已在github上建立项目:https://github.com/ThinkerCodeChina/jsonDB /** +-------------------------------------- ...
- [Javascript] String Padding in Javascript using padStart and padEnd functions
ES2017 added two new string functions. They are padStart and padEndfunctions. In this lesson, we wil ...
- 2014 Unity3d大会的部分总结
一.项目开发.管理和公布策略 1. 四大准则 a. 美术的资源量 b. 美术规范,要依据开发什么样的游戏制定统一的规范,这样尽可能的形成统一的规范.然后程序要协助美 ...
- Hadoop入门进阶步步高(二)-文件夹介绍
二.Hadoop文件夹结构 这里重点介绍几个文件夹bin.conf及lib文件夹. 1.$HADOOP_HOME/bin文件夹 文件名 说明 hadoop 用于运行hadoop脚本命令,被hadoop ...
- codeforces#281 A
脑子有点秀逗 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm ...
- kibana 5.5 源码编译踩坑记录
由于项目需要定制开发kibana,因此需要编译kibana,在开发环境下运行. 注意:必须下载kibana 5.5的源码才能正常编译,下载release或者snapshot版本是不行的,运行npm ...
- django笔记10 cookie整理
感谢武沛齐老师 Alex老师 cookie 没有cookie所有的网站都登录不上 客户端浏览器上的一个文件 {'user':'ljc'} {"user":'zpt'} reques ...