在C++中,程序员可以直接操作内存,给编程增加了不少的灵活性。但是灵活性是有代价的,程序员必须负责自己负责释放自己申请的内存,否则就会出现内存泄露。智能指针就是为了解决这个问题而存在的。它和其他指针没有本质的区别,主要的目的就是为了避免悬挂指针、内存泄露的问题。在这里,我使用对象的应用计数做了一个smart pointer,当一个对象还有引用的时候,就不执行释放内存的操作,当引用计数为0时,就执行内存释放操作,并且将指针重置为NULL。

代码如下:

#include <iostream>
#include <malloc.h> /* desc: A smart pointer to automatic alloc and dellocat memories.
author: justinzhang(uestczhangchao@gmail.com).
time: 2015-1-22 09:44:24 */ template <class T>
class smartPtr { public:
smartPtr() {
ptr = NULL;
refCnt = (unsigned *) malloc(sizeof(unsigned));
} smartPtr( T * pt) { this->ptr = pt;
refCnt = (unsigned *) malloc(sizeof(unsigned));
std::cout << "Enter constructor, refCnt is "<< *refCnt << std::endl;
*refCnt = 1; std::cout << "Leave constructor, refCnt is " << *refCnt << std::endl; } smartPtr(smartPtr<T> &copy) { ptr = copy.ptr; refCnt = copy.refCnt;
std::cout << "Enter copy constructor, refCnt is " << *refCnt << std::endl; ++*refCnt; std::cout << "Leave copy constructor, refCnt is "<< *refCnt << std::endl; } smartPtr<T> & operator=(smartPtr<T> &copy) { std::cout << "Enter operator=, refCnt is "<< *copy.refCnt << std::endl; if(this != &copy) {
ptr = copy.ptr;
refCnt = copy.refCnt;
++*refCnt;
}
std::cout << "Leave operator=, refCnt is " << *refCnt << std::endl;
return *this; } ~smartPtr() { std::cout << "Enter destructor, refCnt is " << *refCnt << std::endl; --*refCnt; if(*refCnt == 0 ) {
std::cout << "In destructor, refCnt is 0 , this pointer will be freed." << std::endl; if( NULL != ptr ) { delete ptr;
ptr = NULL; } if(NULL != refCnt ) {
free(refCnt);
refCnt = NULL;
} } else { std::cout << "Leave destructor, refCnt is " << *refCnt << std::endl;
} } T getValue() { return *ptr;
} protected:
T * ptr;
unsigned *refCnt; }; int main() { int * p = new int[2]; smartPtr<int > intSmart(p) ;
smartPtr<int> copySmart(intSmart); // copy constructor will be called.
smartPtr<int> operatorSmart = intSmart ; // Here the copy consturctor will be called, not the assignment operator.
operatorSmart = intSmart; // Here the assignment operator will be called. return 0; }

运行结果如下:

C++ smart pointer智能指针的更多相关文章

  1. C++2.0新特性(六)——<Smart Pointer(智能指针)之shared_ptr>

    Smart Pointer(智能指针)指的是一类指针,并不是单一某一个指针,它能知道自己被引用的个数以至于在最后一个引用消失时销毁它指向的对象,本文主要介绍C++2.0提供的新东西 一.Smart P ...

  2. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

  3. Smart pointer 智能指针小总结

    Smart pointer line 58之后smart pointer里的计数已经是0,所以会真正释放它引用的对象,调用被引用对象的析构函数.如果继续用指针访问,会出现如下图的内存访问异常.所以说如 ...

  4. C++2.0新特性(八)——<Smart Pointer(智能指针)之unique_ptr>

    一.概念介绍 unique_ptr它是一种在异常发生时可帮助避免资源泄露的smart pointer,实现了独占式拥有的概念,意味着它可确保一个对象和其他相应资源在同一时间只被一个pointer拥有, ...

  5. C++2.0新特性(七)——<Smart Pointer(智能指针)之weak_ptr>

    一.weak_ptr出现的意义 上一节提到过shared_ptr,它会自动释放“不再需要使用的对象”的相应的资源,但是它不是万能的,在某些时候(比如说循环引用),它会显得力不从心,这就是weak_pt ...

  6. Smart Pointer 智能指针

    P76 参考:http://www.cnblogs.com/lanxuezaipiao/p/4132096.html http://blog.csdn.net/hackbuteer1/article/ ...

  7. C++11特性 - Smart Pointers 智能指针

    已经有成千上万的文章讨论这个问题了,所以我只想说:现在能使用的,带引用计数,并且能自动释放内存的智能指针包括以下几种:         unique_ptr: 如果内存资源的所有权不需要共享,就应当使 ...

  8. 智能指针 shared_ptr 解析

    近期正在进行<Effective C++>的第二遍阅读,书里面多个条款涉及到了shared_ptr智能指针,介绍的太分散,学习起来麻烦.写篇blog整理一下. LinJM   @HQU s ...

  9. c/c++ 标准库 智能指针( smart pointer ) 是啥玩意儿

    标准库 智能指针( smart pointer ) 是啥玩意儿 一,为什么有智能指针??? c++程序员需要自己善后自己动态开辟的内存,一旦忘了释放,内存就泄露. 智能指针可以帮助程序员"自 ...

随机推荐

  1. Android学习笔记二:activity的理解

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/7513290.html 一:activity定义了app的页面 一个app有很多个页面组成,一个页面其实就是一个 ...

  2. VIM设置代码折叠

    今天看了一下别人写的程序的源代码,发现是用vim写的,代码中有趣是用vim来折叠代码,一开始我以为是用插件的,后来上网查了查,得出以下使用方面的技巧. 1. 折叠方式 可用选项来设定折叠方式: 可在V ...

  3. simplify-path-字符串处理,去除多余路径

    题目描述 Given an absolute path for a file (Unix-style), simplify it. For example,path ="/home/&quo ...

  4. POJ 2186 强联通分量

    点击打开链接 题意:牛A喜欢牛B,若牛B喜欢牛C,则牛A喜欢牛C,问最后多少牛被其它全部牛喜欢 思路:用强联通分量进行缩点,最后形成的图是有向无环图DAG.而拓扑序的值为DAG的长度,则加一,可是最后 ...

  5. 如何查看2to3.PY的帮助文档

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #如何查看2to3.PY的帮助文档 #http://tieba.baidu.com/p/3939904893 ...

  6. 转 Linux定时执行任务命令at和crontab

    本文介绍在Linux下的两种定时执行任务的方法:at命令,以及crontab服务. (1)at命令 假如我们只是想要让特定任务运行一次,那么,这时候就要用到at监控程序了. 设置at命令很简单,指示定 ...

  7. RHEL7 DNS 服务 unbound 测试

    测试环境: 物理机win10系统,虚拟机软件使用Oracle VirtualBox. rhel1.rusky.com 192.168.100.1 RHEL7(辅DNS) rhel2.rusky.com ...

  8. iOS设置圆角的四种方法

    小小圆角问题,正常情况下,我们不需要过多关心,但当屏幕内比较多的时候,还是有必要了解下性能问题的 一.设置CALayer的cornerRadius 这是最常用的,也是最简单的. cornerRadiu ...

  9. java struts2入门学习---中文验证、对错误消息的分离、结果(result)类型细节配置

    一.需求 利用struts2实现中文验证并对错误消息的抽离. 详细需求:用户登录-->不填写用户名-->页面跳转到用户登录页面,提示用户名必填(以英文和中文两种方式提示)-->填写英 ...

  10. CreateThread与_beginthread, _beginthreadex创建线程的基本概念和区别

    这三个函数都可以创建新的线程,但都是如何创建的呢?当然MSDN文档最权威: Creates a thread to execute within the virtual address space o ...