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 not use pointers or references obtained from the broken smart pointer ​ in C++

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "Why do not use pointers or references obtained from broken smart pointers 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 don't use pointers or references obtained from broken smart pointers in C++?"

R.37: do not use pointers or references obtained from broken smart pointers

Reason (reason)

Violation of this rule is the first reason for the loss of reference count and the occurrence of dangling pointers. The function should rather pass the original pointer and reference down the call chain. You should get the original pointer or reference from the smart pointer that guarantees the existence of the object at the top of the call tree. You need to make sure that smart pointers are not accidentally reset or re-assigned under the call tree.

Note (Note)

To do this, you need to get a local copy of the smart pointer that allows you to lock the object firmly during function and call tree execution.

Example (sample)

Consider the following code:

/ / global (static or heap), or aliased local...

Shared_ptr Groupp =...

Void f (widget& w)

{

G ()

Use (w); / / A

}

Void g ()

{

Groupp =...; / / oops, if this was the last shared_ptr to that widget, destroys the widget

}

The following code should fail the code review:

Void my_code ()

{

/ / BAD: passing pointer or reference obtained from a non-local smart pointer

/ / that could be inadvertently reset somewhere inside f or its callees

F (* glosp)

/ / BAD: same reason, just passing it as a "this" pointer

Groupp-> func ()

}

To correct this problem-get a local copy of the pointer to "keep the reference count" for the call tree.

Void my_code ()

{

/ / cheap: 1 increment covers this entire function and all the call trees below us

Auto pin = Group

/ / GOOD: passing pointer or reference obtained from a local unaliased smart pointer

F (* pin)

/ / GOOD: same reason

Pin- > func ()

} Enforcement (implementation recommendations)

(simple) if the function is called using a pointer or reference obtained from a non-local smart pointer variable (Unique_pointer or Shared_pointer), an alarm is given. The smart pointer alarms when it is a local variable but may be an alias. If the smart pointer is a Shared_pointer, it is recommended that you take a local copy of the smart pointer and then get a pointer or reference from that copy.

At this point, I believe you have a deeper understanding of "Why do not use pointers or references obtained from broken smart pointers in C++?" 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