前言

今天看到一个程序,用到了智能指针,

virtual tmp<volScalarField> rho() const;

借此机会把有关智能指针的知识体系重新梳理一遍


智能指针autoPtr的由来:

首先要说明智能指针本质上是模板类,是对原有指针的改进,相比更安全,



of中的智能指针autoPtr很像原有的auto_ptr,但不是对原有的封装,而是重新写了一遍

of对autoPtr的描述如下:

An auto-pointer similar to the STL auto_ptr but with automatic casting

to a reference to the type and with pointer allocation checking on access.

std::auto_ptr的定义大致如下:

template <typename _Tp>
class auto_ptr
{
private:
_Tp *_M_ptr; public:
explicit auto_ptr(_Tp *__p = 0) throw();
auto_ptr(auto_ptr &__a) throw();
auto_ptr &operator=(auto_ptr &__a) throw();
~auto_ptr(); _Tp &operator*() const throw();
_Tp *operator->() const throw(); _Tp *get() const throw();
_Tp *release() throw();
void reset(_Tp *__p = 0) throw();
};

再看咱of中的autoPtr是何其相似,但实现方法肯定各有千秋

template<class T>
class autoPtr
{
mutable T* ptr_;
public:
typedef T Type;
inline explicit autoPtr(T* = nullptr);
inline autoPtr(const autoPtr<T>&);
inline autoPtr(const autoPtr<T>&, const bool reuse);
inline ~autoPtr(); inline bool empty() const;
inline bool valid() const;
inline T* ptr();
inline void set(T*);
inline void reset(T* = nullptr);
inline void clear(); inline T& operator()();
inline const T& operator()() const;
inline T& operator*();
inline const T& operator*() const;
inline operator const T&() const;
inline T* operator->();
inline const T* operator->() const;
inline void operator=(T*);
inline void operator=(const autoPtr<T>&);
};

在autoPtr中,我们也能看到在autoPtr中加了很多unique_ptr的元素,比如说reset(),

了解of中的智能指针的来源,那么为什么要用智能指针呢,他的应用场景是哪些,下次我们自己写的时候要什么时候用


为什么要用智能指针:

举个例子,比如说我们要实现插值算法,用matlab写,这很简单

result = function(input)

现在我们学习C++了,知道了可以传指针或引用,可以这样写

function(&result, input);

相比之下of更倾向于使用matlab的书写方式

因为简单

不仅是看起来简单,写起来也简单,可以更直观的表达想法

对于没接触过C或C++的人来说,不必了解引用左值右值等一系列知识

在of中写动量方程,

fvVectorMatrix UEqn
(
fvm::ddt(rho, U)
+ fvm::div(rhoPhi, U)
+ turbulence->divDevRhoReff(rho, U)
);

首先这是个类fvVectorMatrix的构造函数,还是个拷贝构造

那这就需要类内部的重载以及函数返回类型都是fvVectorMatrix类

对于需要引入方程的人来说这样写很明显更直观更简单

但是C++作为效率最高的语言,引用这个概念的提出肯定有他的道理

我们一般说引用是什么,很多说是别名,实际上没有说到本质

引用的本质是指针常量,如果换C语言的写法是这样的

int* const rb = &a;

matlab以简单易用著称,但用过matlab的人都知道matlab的效率极低,

本科时候当时不会向量化编程,参加数学建模比赛跑一个循环,跑了整整24小时,笔记本散热也不大行,后来送修主板了

为什么matlab效率低,很关键的一点是matlab一直都是复制拷贝

C/C++指针传地址效率就高很多,况且C++引用的本质就是指针

在简单易用和效率之间,matlab选择了前者,C++选择了后者

openfoam是一个非常强大的张量计算程序,既不能舍弃易用性抬高门槛,又不能反复使用复制拷贝降低效率,稀疏矩阵那么大拷贝来拷贝去算一个程序跑好几年成本太高

openfoam使用智能指针解决了这个问题,看起来不难读懂又能保证效率,这也就回答了为什么要使用智能指针

再回到我们刚刚说的动量方程拷贝构造上,首先在书写方法上依旧是matlab的显式书写方法,但实际上是C++的隐式移动拷贝

可能现在依旧疑惑在哪里看到用指针了,可以打开fvm命名空间的内容

namespace fvm
{ // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // template<class Type>
tmp<fvMatrix<Type>>
d2dt2
(
const GeometricField<Type, fvPatchField, volMesh>& vf
)
{
return fv::d2dt2Scheme<Type>::New
(
vf.mesh(),
vf.mesh().d2dt2Scheme("d2dt2(" + vf.name() + ')')
).ref().fvmD2dt2(vf);//这里返回的可是fvMatrix<Type>类型指针
} template<class Type>
tmp<fvMatrix<Type>>
d2dt2
(
const dimensionedScalar& rho,
const GeometricField<Type, fvPatchField, volMesh>& vf
)
{
return fv::d2dt2Scheme<Type>::New
(
vf.mesh(),
vf.mesh().d2dt2Scheme("d2dt2(" + rho.name() + ',' + vf.name() + ')')
).ref().fvmD2dt2(rho, vf);
} template<class Type>
tmp<fvMatrix<Type>>
d2dt2
(
const volScalarField& rho,
const GeometricField<Type, fvPatchField, volMesh>& vf
)
{
return fv::d2dt2Scheme<Type>::New
(
vf.mesh(),
vf.mesh().d2dt2Scheme("d2dt2(" + rho.name() + ',' + vf.name() + ')')
).ref().fvmD2dt2(rho, vf);
} // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace fvm

看到了嘛,随便一个遍布tmp智能指针,在指针赋值时就已经完成了类的初始化

但又因为只是指针,可以用显式的方法去写,只要保证返回类型相同即可

fvm += fvc::surfaceIntegrate
(
faceFlux*tinterpScheme_().correction(vf)
);

表面上是大型矩阵相加减,实际上是智能指针这个地址在执行重载,极大的提升了效率

打个比方,这就像高启强要和赵立冬或孟德海商量一件事,赵这个级别的不方便出面,出面的都是龚开疆或王秘书这样的人,又能传达指示又不消耗大量资源,好处就是双方都留有余地

智能指针智能的点就在于不需要或者出问题的时候能自动销毁,打开相关析构函数

template<class T>
inline Foam::tmp<T>::~tmp()
{
clear();
}
template<class T>
inline void Foam::tmp<T>::clear() const
{
if (isTmp() && ptr_)
{
if (ptr_->unique())
{
delete ptr_;
ptr_ = 0;
}
else
{
ptr_->operator--();
ptr_ = 0;
}
}
}

tmp析构时对该智能指针进行了delete,

记得狂飙里调查组一来最先销毁的也是龚开疆这个智能指针,,,

这样openfoam无需g++ -o优化也能有很好的运行效率


autoPtr与tmp的使用场合与区别

在openfoam中,autoPtr是强引用类型智能指针,tmp是弱引用类型智能指针

那我们在什么时候使用autoPtr以及tmp呢

autoPtr多使用在transport models ,boundry conditions,discretization schemes,turbulenceModel,interpolation schemes,gradient schemes或fvOptions这种动态多态中,更适合析构频次高的地方,智能指针autoPtr能够自动析构,因而被广泛使用

autoPtr<incompressible::RASModel> turbulence
(
incompressible::RASModel::New(U, phi, laminarTransport)
);

autoPtr一旦有所指向只能移动,不能复制,同名同类型只能指向一个对象

再说tmp,之前有博客说tmp类似shared_ptr,实际上tmp的自我介绍中并没有像autoPtr一样提及相关类auto_ptr,和shared_ptr也不是继承关系,但实现功能很接近

A class for managing temporary objects.

tmp的自我介绍中说是管理临时变量的类,这个介绍更像是我们日常做的副本,就像我现在做的博客,害怕自己忘做份笔记,日后翻看,当然这个博客的建立首先是自己做好了理解,因而类似的,tmp的构造需要autoPtr在前面已经做好了指定,tmp配合进行副本引用

tmp的销毁和shared_ptr一致,具体可以见shared_ptr


一起探索openfoam也是相当有趣的一件事,非常欢迎私信讨论

指正的价值要比打赏更重要,下面是个人联系方式,希望能结交到志同道合的朋友

最新文章

  1. 到处都是坑的微信支付V3之 微信支付回调页面
  2. 详解Javascript的继承实现(二)
  3. IOS圆头像
  4. Uva1398 Meteor
  5. c/c++面试题(4)字符串翻转/打印任意进制格式/类型转换
  6. C#(二维数组/集合)
  7. Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟
  8. ubuntu12.04下txt文件乱码如何解决
  9. SetUID、SetGID中的大小写Ss和Sticky bit中的大小写Tt
  10. 用STRACE解决公司真实故障一例
  11. C++快速排序实现(quicksort)
  12. 5.非关系数据库(Nosql)它mongodb:创建一个集合,导出和导入备份, 数据恢复,进出口
  13. Windows Nodejs 安装教程
  14. Python爬虫小实践:爬取任意CSDN博客所有文章的文字内容(或可改写为保存其他的元素),间接增加博客访问量
  15. 利用ELK分析Nginx日志生产实战(高清多图)
  16. bzoj3276磁力 两种要求下的最大值:分块or线段树+拓扑
  17. Android中intent的分类及使用
  18. for循环,列表,元组
  19. BOM - 浏览器API
  20. sort.js

热门文章

  1. selenium常用配置
  2. eval解析的函数传参 object array
  3. week_11
  4. Windows上将linux目录映射网络驱动器
  5. [常用工具] PyAutoGUI使用教程
  6. [python] Python二维码生成器qrcode库入门
  7. [OpenCV实战]30 使用OpenCV实现图像孔洞填充
  8. [WPF]ICommand最佳使用方法
  9. angular--路由导航三种方法
  10. 字符串拼接输出-Predicate接口