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 should you never use original pointers or references to transfer ownership in C++

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

Share

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

This article mainly explains "Why should you never use original pointers or references to transfer ownership in C++?" interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn why you should never use raw pointers or references to transfer ownership in C++.

Reason (reason)

Leaks or premature destruction can occur if there is any doubt as to whether an object belongs to the caller or the callee.

Example (sample)

Consider: (consider)

X* compute (args) / / don't {X* res = new X {}; / /. Return res;}

Who should delete the returned X? This problem is difficult to determine when compute returns a reference. Consider returning a value (use mobile syntax if the result is large)

Translator's note: mobile syntax refers to the right-value reference and std::move introduced by Clover 11.

Vector compute (args) / / good {vector res (10000); / / Return res;}

Other options: use smart pointers to transfer ownership, such as unique_ptr (for exclusive ownership) and shared_prt (for shared ownership). However, this approach is slightly less elegant and efficient than directly returning the object itself, so smart pointers should be needed only when you need to refer to semantics.

Other options: sometimes old code cannot be modified because of ABI compatibility requirements or the need to avoid resource leaks. In this case, use the owner form provided by the guidelines support library to mark the pointer to control ownership.

Translator's note: ABI, the application binary interface (Application Binary Interface) provided by the operating system

Owner compute (args) / / It is now clear that ownership is transferred {owner res = new X {}; / /. Return res;}

This tells the analysis tool that res is the owner. That is, its value must be deleted or transferred to another owner, as is done here when the result is returned through return.

Owner is used in a similar way when implementing resource handles.

Note (Note)

All objects passed through the original pointer (or iterator) are assumed to be owned by the caller, so its life cycle is also managed by the caller. To put it another way: relatively speaking, ownership transfer API is relatively small compared to pointer-passing API, so there is no ownership transfer by default.

Enforcement (implementation recommendations)

(Simple) Warn on delete of a raw pointer that is not an owner. Suggest use of standard-library resource handle or use of owner.

(simple) alarm when deleting the original pointer instead of owner. It is recommended that you use a resource handle or owner in the standard library

(Simple) Warn on failure to either reset or explicitly delete an owner pointer on every code path.

(simple) alarm if the reset or deletion of the owner pointer on any code path fails.

(Simple) Warn if the return value of new or a function call with an owner return value is assigned to a raw pointer or non-owner reference.

(simple) alarm if the result of a new or function call that returns owner is assigned to the original pointer or a non-owner reference.

At this point, I believe you have a deeper understanding of "Why C++ should never use original pointers or references to transfer ownership". You might as well do it in practice. 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