Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why does C++ perform explicit resource allocation at most once in an expression?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains why C++ performs explicit resource allocation at most once in an expression, which interested friends may wish to have a look at. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn why C++ can only perform explicit resource allocation at most once in an expression.

R.13: perform explicit resource allocation at most once in an expression

Reason (reason)

If you perform two (or more) resource allocations in an expression, the order in which subexpressions, including function parameters, are executed is undefined, which may lead to resource leakage.

Example (sample)

Void fun (shared_ptr sp1, shared_ptr sp2)

The function may be called as follows:

/ / BAD: potential leak

Fun (shared_ptr (new Widget (a, b)), shared_ptr (new Widget (c, d)

Because the compiler may adjust the execution order of two expressions that build function parameters, this code can go wrong when an exception occurs. Typically, the compiler interlaces two expressions: (using new) memory allocation for two objects may occur first, followed by calls to the constructors of two Widget. If one calls one constructor to throw an exception, the other will never be released.

There is a simple solution to this imperceptible problem: never perform an explicit resource allocation more than twice (including twice) in an expression. For example:

Shared_ptr sp1 (new Widget (a, b)); / / Better, but messy

Fun (sp1, new Widget (c, d))

The best solution is to use a factory method that returns management objects to avoid explicit resource allocation altogether.

Fun (make_shared (a, b), make_shared (c, d); / / Best

If it doesn't already exist, write your own factory wrapper class.

Enforcement (implementation recommendations)

Tags have multiple expressions for explicitly allocating resources (the question is: how many cases can we identify when explicitly allocating resources? )

Now that you have a better understanding of why C++ only performs explicit resource allocation at most once in an expression, you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report