Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Why should C++ avoid displaying calls to new and delete

Shulou Source: shulou.com Published: 2022-06-01 19:23:56 09月20日 Update

This article mainly explains "why C++ should avoid displaying and calling new and delete". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn "why C++ avoids displaying and calling new and delete".

R.11: avoid displaying calls to new and delete

Reason (reason)

The pointer returned by new should be managed by the handle of the resource (the one responsible for calling delete). If the pointer returned by new is assigned to the original pointer, the object may have a memory leak.

Note (Note)

In large-scale programs, exposed deletions (calling delete in the application code rather than handing it over to resource management) can trigger bug: if there are N times of delete, how can you be sure that you don't need it once? Bug may be potential: it may occur after some maintenance. If there is a direct new operation, you may need to invoke the direct delete operation somewhere, so a bug may be thrown.

Enforcement (implementation recommendations)

(simple) warn of any explicit calls to new and delete. Make_unique is recommended.

Thank you for your reading, the above is "Why C++ should avoid displaying calls to new and delete" content, after the study of this article, I believe you have a deeper understanding of why C++ should avoid displaying calls to new and delete, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Caching pointers learning content suggestions situations resources management raw code memory causes handles large-scale objects that is ideas articles more potential Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia macOS Shulou Tech Info Docker vpn