本文共 1331 字,大约阅读时间需要 4 分钟。
ES.61: Delete arrays using delete[] and non-arrays using delete
ES.61:使用delete[]销毁数组,使用delete销毁对象
Reason(原因)
That's what the language requires and mistakes can lead to resource release errors and/or memory corruption.
这是C++语言的要求,如果用错会导致资源释放错误或者内存破坏。
Example, bad(反面示例)
void f(int n){ auto p = new X[n]; // n default constructed Xs // ... delete p; // error: just delete the object p, rather than delete the array p[]}
Note(注意)
This example not only violates the no naked new rule as in the previous example, it has many more problems.
示例代码不仅违反了前面示例中的禁止暴露的new规则,还有更多其他错误。
Enforcement(实施建议)
-
If the new and the delete are in the same scope, mistakes can be flagged.
-
如果new和delete同属一个作用域,错误可以被标记。
-
If the new and the delete are in a constructor/destructor pair, mistakes can be flagged.
-
如果new和delete分别位于构造函数和析构函数,错误可以被标记。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es61-delete-arrays-using-delete-and-non-arrays-using-delete
新书介绍
以下是本人3月份出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?欢迎点赞并分享给更多的人。
阅读更多更新文章,请关注微信公众号【面向对象思考】
转载地址:https://oothinking.blog.csdn.net/article/details/107346155 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!