C++核心准则讨论:提供强大的资源安全性;也就是说,永远不要泄漏任何您认为是资源的东西
发布日期:2021-07-01 05:31:03 浏览次数:2 分类:技术文章

本文共 2165 字,大约阅读时间需要 7 分钟。

Discussion: Provide strong resource safety; that is, never leak anything that you think of as a resource

讨论:提供强大的资源安全性;也就是说,永远不要泄漏任何您认为是资源的东西

 

Reason(原因)

Prevent leaks. Leaks can lead to performance degradation, mysterious error, system crashes, and security violations.

原因防止泄漏。泄漏会导致性能下降,神秘错误,系统崩溃和违反安全性。

Alternative formulation: Have every resource represented as an object of some class managing its lifetime.

替代方案:将每种资源表示为管理其寿命的某个类的对象。

 

Example(示例)

template
class Vector {private: T* elem; // sz elements on the free store, owned by the class object int sz; // ...};

This class is a resource handle. It manages the lifetime of the Ts. To do so, Vector must define or delete the set of special operations (constructors, a destructor, etc.).

此类是资源句柄。它管理T们的寿命。为此,Vector必须定义或删除特殊操作集(构造函数,析构函数等)。

 

Example(原因)

??? "odd" non-memory resource ???

Enforcement(实施建议)

The basic technique for preventing leaks is to have every resource owned by a resource handle with a suitable destructor. A checker can find "naked news". Given a list of C-style allocation functions (e.g., fopen()), a checker can also find uses that are not managed by a resource handle. In general, "naked pointers" can be viewed with suspicion, flagged, and/or analyzed. A complete list of resources cannot be generated without human input (the definition of "a resource" is necessarily too general), but a tool can be "parameterized" with a resource list.

防止泄漏的基本技术是用带有合适的析构函数的资源句柄管理每个资源。检查器可以找到“暴露的new S”操作。给定一系列C样式的分配函数(例如fopen()),检查器还可以查找不受资源句柄管理的用法。通常,可以带着“疑问”查看,标记和/或分析“裸指针”。没有人工输入就无法生成完整的资源列表(“资源”的定义必然太笼统),但是可以使用资源列表对工具进行“参数化”。

 

原文链接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#discussion-provide-strong-resource-safety-that-is-never-leak-anything-that-you-think-of-as-a-resource

新书介绍

是作者最近出版的新书,拜托多多关注!

图片

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。

 


 

觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

 

图片

 

转载地址:https://oothinking.blog.csdn.net/article/details/115800106 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++核心准则讨论:持有没有被句柄管理的资源时切勿抛出异常
下一篇:C++核心准则讨论:保持定义复制,移动和析构函数的一致性

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年05月03日 16时31分01秒