auto_ptr & share_ptr & unique_ptr
Using auto_ptr, you don’t need think about the memory deallocation, but there are also many arguments about it because of auto_ptr ownership translation.
void AutoPtrTest(void)
{
std::auto_ptr<int> p1 (new int());
//pass ownership
std::auto_ptr<int> p2 = p1;
std::cout << "p1 will raise exception:" << *p1 << std::endl;
std::cout << "p2 is ok: " << *p2 << std::endl;
}
In C++11, the auto_ptr is deprecated. The shared_ptr and unique_ptr are designed which have more security and comprehensive.
void AutoPtrTest(void)
{
std::auto_ptr<int> p1 (new int());
//pass ownership
std::auto_ptr<int> p2 = p1; //here has a warning in GCC
//std::cout << "p1 will raise exception:" << *p1 << std::endl;
std::cout << "p2 is ok: " << *p2 << std::endl; std::shared_ptr<int> s1 (new int());
std::shared_ptr<int> s2 = s1; //share it. point to same address
std::cout << "s1 is ok:" << *s1 << std::endl;
std::cout << "s2 is ok: " << *s2 << std::endl; std::unique_ptr<int> u1 (new int());
// std::unique_ptr<int> u2 = u1; //cannot be compiled
std::cout << "u1 is ok:" << *u1 << std::endl;
// std::cout << "s2 is ok: " << *s2 << std::endl;
}
In conclusion, auto_ptr could be replaced by shared_ptr or unique_ptr depending upon situation.
auto_ptr & share_ptr & unique_ptr的更多相关文章
- C++智能指针: auto_ptr, shared_ptr, unique_ptr, weak_ptr
本文参考C++智能指针简单剖析 内存泄露 我们知道一个对象(变量)的生命周期结束的时候, 会自动释放掉其占用的内存(例如局部变量在包含它的第一个括号结束的时候自动释放掉内存) int main () ...
- C++11智能指针 share_ptr,unique_ptr,weak_ptr用法
0x01 智能指针简介 所谓智能指针(smart pointer)就是智能/自动化的管理指针所指向的动态资源的释放.它是存储指向动态分配(堆)对象指针的类,用于生存期控制,能够确保自动正确的销毁动 ...
- stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结
stl中auto_ptr,unique_ptr,shared_ptr,weak_ptr四种智能指针使用总结 1. auto_ptrauto_ptr主要是用来解决资源自动释放的问题,比如如下代码:voi ...
- auto_ptr, unique_ptr, shared_ptr and weak_ptr智能指针讲解
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- c++智能指针(unique_ptr 、shared_ptr、weak_ptr、auto_ptr)
一.前序 什么是智能指针? ——是一个类,用来存储指针(指向动态分配对象也就是堆中对象的的指针). c++的内存管理是让很多人头疼的事,当我们写一个new语句时,一般就会立即把delete语句直接也写 ...
- 【校招面试 之 C/C++】第25题 C++ 智能指针(一)之 auto_ptr
1.智能指针背后的设计思想 我们先来看一个简单的例子: void remodel(std::string & str) { std::string * ps = new std::string ...
- C++智能指针 unique_ptr
C++智能指针 unique_ptr unique_ptr 独占所指向的对象, 同一时刻只能有一个 unique_ptr 指向给定对象(通过禁止拷贝语义, 只有移动语义来实现), 定义于 memory ...
- C++11新特性总结 (二)
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
- c++基础 使用智能指针
三个智能指针模板(auto_ptr.unique_ptr和shard_ptr)都定义了类似指针的对象(c++11已将auto_ptr摒弃),可以将new获得(直接或间接) 的地址赋给这种对象.当智能指 ...
随机推荐
- linux系统中不同颜色的文件夹及根目录介绍
文件颜色的代表含义: 蓝色:目录 绿色:可执行文件 红色:压缩文件 蓝绿色:链接文件 灰色:其他文件 黄色:设备文件,其中包括block,char,fifo. 白色:表示普通文件 红色闪烁:表示链 ...
- :复合模式:duck
#ifndef __QUAKEABLE_H__ #define __QUAKEABLE_H__ #include <iostream> #include <vector> us ...
- Cracking The Coding Interview 5.6
//Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g. ...
- jmeter源码导入eclipse并执行
由于JMeter纯Java开发,界面也是基于Swing或AWT搞出来的,所以想更深层次的去了解这款工具或对于想了解JMeter插件开发或二次开发的童鞋们来说,读读JMeter的源码估计是必不可少的,所 ...
- 用servlet实现用户登录案例
以下实现登录窗口 Login.jsp <!--Login.jsp--> <%@ page language="java" import="java.ut ...
- matlabR2017安装
安装教程参考: https://blog.csdn.net/m0_37638031/article/details/78982498
- 51nod1042
给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次(10,11,12,13,14,15,16,17,18,19,其中11包括2个1),其余数字各出现1次. Inp ...
- ::selection 选择器
使被选中的文本成为红色:::selection { color:#ff0000; } ::-moz-selection { color:#ff0000; }
- MySql笔记之数据备份与还原
MySQL数据备份.还原与迁移 一.数据备份------mysqldump 1.语法: mysqldump -u user(用户名)-h host(登录用户的主机名称)-p password(登录密码 ...
- python 时间戳转换格式
1.简介 在编写代码时,往往涉及时间.日期.时间戳的相互转换. 2.示例 # 引入模块 import time, datetime 2.1 str类型的日期转换为时间戳 1 # 字符类型的时间 2 t ...