在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. SpringBoot使用JSP渲染页面

    1.pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  2. 【Zookeeper】源码分析之持久化(三)之FileTxnSnapLog

    一.前言 前面分析了FileSnap,接着继续分析FileTxnSnapLog源码,其封装了TxnLog和SnapShot,其在持久化过程中是一个帮助类. 二.FileTxnSnapLog源码分析 2 ...

  3. 元素高度、宽度获取 style currentStyle getComputedStyle getBoundingClientRect

    1.示例代码 (1)html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  4. CSS拾遗

    1:CSS样式的声明 选择符{ 属性:值; 属性:值; ... } 其中,选择符有: 标签选择器:标签名{样式} 类选择器: .类名{样式} ID选择器:  #ID名{样式} 另外:样式属性的书写格式 ...

  5. JSP之include动态包含与静态包含

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6044676.html JSP中,include是一个经常用到的标签.当应用程序中所有的页面的某些部分(如标题. ...

  6. 关于RSA加密算法的工具类

    关于RSA加密算法的工具类 最近在捣鼓SSO(单点登录),就是一个在应用(系统)登录之后,当切换其他应用(系统)的时候,可以省去登录,提高用户的使用的便捷.(具体有时间在写) 期间涉及的安全问题,发送 ...

  7. mysql统计函数

    数据记录统计函数: AVG(字段名) 得出一个表格栏平均值 COUNT(*|字段名) 对数据行数的统计或对某一栏有值的数据行数统计 MAX(字段名) 取得一个表格栏最大的值 MIN(字段名) 取得一个 ...

  8. Redis从入门到精通:中级篇(转)

    原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...

  9. vs code 问题:preLaunchTask“build”已终止,退出代码为 1。解决办法

    菜单:任务-配置任务 改为如下: { // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation ab ...

  10. UltraEdit编辑器|UE

    目前对我而言,还是比较喜欢editplus/notepad++. 注册码: UltraEdit-32 v15.00注册码:free userGGCDP-KIOGN-KQHLZ-RNCSK-KKKHZ- ...