2014-04-25 20:27

题目:实现一个能够通过引用计数来实现自动回收数据的智能指针,用C++,不是java。

解法:这题真心牛,我的第一反应是发呆,因为对引用计数的了解仅限于这个名词,完全没办法建立模型。之后仔细把题解读了两遍之后,照样敲了一遍代码。然后边调试边阅读才有了些理解。引用计数有四点需要注意:1. 引用计数是个非负整数。2. 引用计数是对于一个实体变量的,所有指向这个实体的指针共用这个计数,所以就有了代码中的那种写法。3. 指针进行析构的时候,引用计数减一。4. 增加一个指针的时候,引用计数加一。

代码:

 // 13.8 Implement a smart pointer class, which is capable of automatic garbage collection. Count of reference is the key to this problem.
// Answer:
// int *ref_count;
// referece count must be int *, instead of int. Think about why.
// The destructor function is called for (*ref_count) times, only at the last time the real data is freed.
#include <iostream>
using namespace std; template <class T>
class Pointer {
public:
Pointer(T *ptr) {
data = ptr;
ref_count = new int();
}; Pointer(Pointer<T> &p) {
data = p.data;
ref_count = p.ref_count;
++(*ref_count);
}; Pointer<T>& operator = (Pointer<T> &p) {
if (this == &p) {
// nothing to do.
return *this;
}
if (*ref_count > ) {
remove();
}
data = p.data;
ref_count = p.ref_count;
++(*ref_count); return *this;
}; T getData() {
return *data;
}; void setData(const T &val) {
*data = val;
}; ~Pointer() {
remove();
};
protected:
T *data;
int *ref_count; void remove() {
--(*ref_count);
if (*ref_count == ) {
// if the reference count becomes 0, the data is never to be found again.
// it must be freed.
delete data;
data = nullptr;
delete ref_count;
ref_count = nullptr;
}
};
}; int main()
{
int *ptr = new int();
Pointer<int> p1(ptr);
Pointer<int> p2(p1); cout << p1.getData() << endl;
p2.setData();
cout << p2.getData() << endl; return ;
}

《Cracking the Coding Interview》——第13章:C和C++——题目8的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  4. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  8. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  9. 《Cracking the Coding Interview》——第17章:普通题——题目13

    2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...

  10. 《Cracking the Coding Interview》——第13章:C和C++——题目10

    2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...

随机推荐

  1. MAC读取希捷移动硬盘ntfs

    希捷提供了mac读取ntfs磁盘的软件,Paragon. 搜索关键词 "希捷" "mac" 或者通过以下链接进入 https://www.seagate.com ...

  2. ffmpeg:编解码过程,基本用法

    1  术语: 什么是影片?其实就是一组(很多张)图片,时间间隔很小的连续展示出来,人们就觉得画面中的人物在动,这就是影片.那电影的实质就是N多张图片的集合.那 每张图片和帧又有什么关系呢?事实上,如果 ...

  3. Invalid MyEclipse License - Discontinuing this MyEclipse operation. 出现这个错误怎么改正?

    Invalid MyEclipse License - Discontinuing this MyEclipse operation这句话的意思是无效的许可证-停用此MyEclipse操作入门就是你的 ...

  4. E. New Reform_贪心,深搜,广搜。

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Python中的__name__和__main__含义详解

    1背景 在写Python代码和看Python代码时,我们常常可以看到这样的代码: ? 1 2 3 4 5 def main():     ......   if __name == "__m ...

  6. failed to bind pixmap to texture

    问题描述:我用的是Ubuntue的操作系统,终端突然挂了.我重启了一下电脑,就进不去系统了. 日志信息:  failed to bind pixmap to texture 原因: 界面管理工具坏了, ...

  7. 缓冲区溢出实战教程系列(三):利用OllyDbg了解程序运行机制

    想要进行缓冲区溢出的分析与利用,当然就要懂得程序运行的机制.今天我们就用动态分析神器ollydbg来了解一下在windows下程序是如何运行的. 戳这里看之前发布的文章: 缓冲区溢出实战教程系列(一) ...

  8. CSS 负边距读后感

    最近看到一篇讲解CSS 负边距的文章: http://segmentfault.com/a/1190000003750411?utm_source=Weibo&utm_medium=share ...

  9. 太阳地球月亮运行动画(使用@keyframes)

    闲来无事的demo <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  10. BZOJ1202: [HNOI2005]狡猾的商人(带权并查集)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4577  Solved: 2249[Submit][Status][Discuss] Descript ...