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创建的动态内存,它是这块内存的拥有者,一块内存不能同一时候被分给两个拥有者.当 ...
随机推荐
- Codeforces 13C Sequence dp
题目链接:http://codeforces.com/problemset/problem/13/C 题意: 给定n长的序列 每次操作能够给每一个数++或-- 问最少须要几步操作使得序列变为非递减序列 ...
- nyoj 628 小媛在努力 【搜索】
第一次是直接建一个10^7的数组 结果 内存大的要死.! 是不是能够不建数组 这下好了 小媛在努力 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 在多媒体数据处理 ...
- Process Monitor
https://en.wikipedia.org/wiki/Process_Monitor Process Monitor is a free tool from Windows Sysinterna ...
- codeblocks开源的c、c++编译器,小巧方便
1.下载带gun的版本 2.设置编译的位置 3.创建项目 4.执行项目 有意思的开源的c编译器 ~~~
- android常用自动化测试框架
目录: Monkey MonkeyRunner Instrumentation UiAutomator Espresso Selendroid Robotium Athrun Appium Monke ...
- Creating a New Master Page in SharePoint 2013
Creating a New Master Page in SharePoint 2013 This article explains how to create a Master Page in S ...
- BZOJ 4289: PA2012 Tax(最短路)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 755 Solved: 240[Submit][Status][Discuss] Descriptio ...
- category和关联对象
如上所见,我们知道在category里面是无法为category添加实例变量的.但是我们很多时候需要在category中添加和对象关联的值,这个时候可以求助关联对象来实现. MyClass+Categ ...
- 使用3ds Max制作卡通狗教程
使用软件::3ds Max 软件下载:http://www.xy3dsmax.com/xiazai.html 全教程完,学完记得交作业.如果本教程对您有所帮助,请推荐给你的朋友. 全教程完,学完记得交 ...
- Python 计算相似度
#计算相似度 #欧式距离 # npvec1, npvec2 = np.array(det_a), np.array(det_b) # similirity=math.sqrt(((npvec1 - n ...