32.智能指针auto_ptr
#include <iostream>
#include <memory>
#include <string>
#include <vector>
using namespace std; struct my
{
int x;
int y;
}; //智能指针主要用于解决内存泄漏,拥有常规指针一样的使用,重载* ->运算符
void run1()
{
//void *p = malloc(1024 * 1024 * 100);
//智能指针,检测到没有使用就会自动释放
// auto_ptr<int>myp(new int[1024 * 1024 * 400]);
//*myp;//根据指针去内存,重载* auto_ptr<my> my1(new my[]);
cout << my1->x << endl;
} void run()
{
//指针只能用构造函数初始化,用explicit拒绝auto_ptr<string> p = new string[100] 初始化
//智能指针可以创建指向STL的指针,但只能创建一个
auto_ptr<string> p(new string);
//创建多个会出错
//auto_ptr<string> p( new string[10] );
*p = ""; //string *p3 = new string[10]; //智能指针是浅拷贝(通过计数的方式决定所引用的空间是不是可以释放)
auto_ptr<string> p2(p);
cout << *p2 << endl;
//STL可以包含智能指针
vector<auto_ptr<int>> myv;
myv.push_back(auto_ptr<int>(new int[]));
} void main()
{
while ()
{
run();
}
cin.get();
}
32.智能指针auto_ptr的更多相关文章
- C++智能指针(auto_ptr)详解
智能指针(auto_ptr) 这个名字听起来很酷是不是?其实auto_ptr 只是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势,但也有其局限.本文总结的8个问题足 ...
- 自己动手实现智能指针auto_ptr
面试的时候,我们经常会被问到如何自己动手实现智能指针auto_ptr.今天我就一边参考STL库中的源代码,一边将auto_ptr的实现敲一遍. auto_ptr归根到底是一个模版类,那么这个类要实现哪 ...
- C++ 智能指针auto_ptr
template<class T> class auto_ptr { public: ); // Item M5 有“explicitfor”// 的描述 template<clas ...
- 关于智能指针auto_ptr
智能指针auto_ptr和shared_ptr也是面试中经常被问到的一个 感觉看auto_ptr的源码反而更加容易理解一些,因为源码的代码量并不大,而且比较容易理解. 本篇主要介绍auto_ptr 其 ...
- C++中的智能指针(auto_ptr)
实际上auto_ptr 仅仅是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势.使用它不必每次都手动调用delete去释放内存.当然有利也有弊,也不是全然完美的. 本 ...
- 【C++】智能指针auto_ptr简单的实现
//[C++]智能指针auto_ptr简单的实现 #include <iostream> using namespace std; template <class _Ty> c ...
- 智能指针auto_ptr & shared_ptr
转载:智能指针auto_ptr 很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生 ...
- C++智能指针 auto_ptr
C++智能指针 auto_ptr auto_ptr 是一个轻量级的智能指针, 定义于 memory (非memory.h)中, 命名空间为 std. auto_ptr 适合用来管理生命周期比较短或者不 ...
- C++智能指针--auto_ptr指针
auto_ptr是C++标准库提供的类模板,头文件<memory>,auto_ptr对象通过初始化指向由new创建的动态内存,它是这块内存的拥有者,一块内存不能同一时候被分给两个拥有者.当 ...
随机推荐
- selenium的报错信息:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
报错信息:selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class ...
- Oracle Table Function
Oracle Table Function在Oracle9i时引入.完美的兼容了view和存储过程的长处: 应用举例: 1.Table()函数: set feedback off create or ...
- group_concat函数
- class.forName的官方使用方法说明
原文地址:http://yanwushu.sinaapp.com/class_forname/ 使用jdbc方式链接数据库时会常常看到这句代码:Class.forName(String classNa ...
- Cordova 5 架构学习 Weinre远程调试技术
手机上的页面不像桌面开发这么方便调试.能够使用Weinre进行远程调试以方便开发.本文介绍windows下的安装与使用. 安装 使用npm安装.能够执行: ###npm config set regi ...
- POJ 1944 并查集(模拟)
思路: 肯定是要枚举断点的..就看枚举完断点以后怎么处理了-- 1.用类似并查集的思想- f[x]=max(f[x],y)表示x和y相连(一定要注意取max,,,血的教训) 复杂度O(np) 2.猥琐 ...
- Java8新特性 利用流和Lambda表达式对List集合进行处理
Lambda表达式处理List 最近在做项目的过程中经常会接触到 lambda 表达式,随后发现它基本上可以替代所有 for 循环,包括增强for循环.也就是我认为,绝大部分的for循环都可以用 la ...
- shell编程笔记1
参考文章:1 http://blog.csdn.net/wuwenxiang91322/article/details/9259877 通过chmod改变文件权限 补充知识: 1Linux文件的三 ...
- Swift 4.0:访问级别(访问控制)
基础篇 注: 下文中所提及的类和类型为Class, Enum和Struct Swift中的访问级别有以下五种: open: 公开权限, 最高的权限, 可以被其他模块访问, 继承及复写. public: ...
- WordPress 增加 keywords 和 description
WordPress 增加 keywords 和 description . <?php $keywords = '798资源网'; $description = '798资源网'; //文章页 ...