C++核心准则C.149:使用unique_ptr或者shared_ptr避免忘记销毁使用...
C.149: Use unique_ptr or shared_ptr to avoid forgetting to delete objects created using new
C.149:使用unique_ptr或者shared_ptr避免忘记销毁使用new创建的对象
Reason(原因)
Avoid resource leaks.
避免资源泄露。
Example(示例)
void use(int i)
{
auto p = new int {7}; // bad: initialize local pointers with new
auto q = make_unique(9); // ok: guarantee the release of the memory-allocated for 9
if (0 < i) return; // maybe return and leak
delete p; // too late
}
Enforcement(实施建议)
Flag initialization of a naked pointer with the result of a new
提示使用new的结果初始化裸指针的情况。
Flag delete of local variable
标记销毁局部变量的情况。
原文链接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c149-use-unique_ptr-or-shared_ptr-to-avoid-forgetting-to-delete-objects-created-using-new
觉得本文有帮助?请分享给更多人。
关注【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
评论