《Cracking the Coding Interview》——第13章:C和C++——题目8
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的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 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 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
随机推荐
- prop & attr
<input id="chk1" type="checkbox" />是否可见 <input id="chk2" type ...
- pat甲级1107
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- 数据结构与算法分析java——线性表2(ArrarList )
ArrayList ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess, C ...
- 【JavaScript 封装库】BETA 4.0 测试版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...
- 使用browsermob代理出现错误java.lang.NoClassDefFoundError: org/littleshoot/proxy/HttpFiltersSource
使用browsermob代理做埋点数据,maven配置的包如下 <dependency> <groupId>net.lightbody.bmp</groupId> ...
- 简单介绍Spring是什么?
对于面试者回答什么是Spring,这个问题占6分分值,分值点分布:1.Spring的核心是一个轻量级(Lightweight)的容器(Container).2.Spring是实现IoC(Inversi ...
- 第46章 DCMI—OV5640摄像头—零死角玩转STM32-F429系列
第46章 DCMI—OV5640摄像头 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com ...
- android design 新控件
转载请标明出处: http://blog.csdn.net/forezp/article/details/51873137 本文出自方志朋的博客 最近在研究android 开发的新控件,包括drawe ...
- [转]C++ Template
引言 模板(Template)指C++程序设计设计语言中采用类型作为参数的程序设计,支持通用程序设计.C++ 的标准库提供许多有用的函数大多结合了模板的观念,如STL以及IO Stream. 函数模板 ...
- [vijos p1028] 魔族密码
描述 风之子刚走进他的考场,就……花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花)风之子:我呕……(杀死人的眼神)快说题目!否则……-_-###花花:……咦~~好冷~~我们现在要 ...