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

How does C++ use not_null to define pointers that cannot be null?

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "C++ how to use not_null to define pointers that cannot be empty", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "C++ how to use not_null to define pointers that cannot be empty".

I.12: Declare a pointer that must not be null as not_null (defines a pointer that cannot be null in not_null) Reason (reason)

To help avoid dereferencing nullptr errors. To improve performance by avoiding redundant checks for nullptr.

To prevent dereferencing nullptr errors. To avoid unnecessary null pointer checking to improve performance.

Translator's note: dereferencing is the operation of adding * before the pointer to get the content.

Example (sample)

Int length (const char* p); / / it is not clear whether length (nullptr) is valid

Length (nullptr); / / OK?

Int length (not_null p); / / better: we can assume that p cannot be nullptr

Int length (const char* p); / / we must assume that p can be nullptr

By stating the intent in source, implementers and tools can provide better diagnostics, such as finding some classes of errors through static analysis, and perform optimizations, such as removing branches and null tests.

By explaining the purpose in the code, implementers and tools can provide better diagnosis, such as finding certain types of errors through static analysis, and performing performance optimizations, such as removing branches and null judgments.

Translator's note: refers to the use of not_null in the above code.

Note (Note)

Not_null is defined in the guidelines support library.

Not_null is defined in the guidelines support library.

Translator's note: actively use the rule base.

Note (Note)

The assumption that the pointer to char pointed to a C-style string (a zero-terminated string of characters) was still implicit, and a potential source of confusion and errors. Use czstring in preference to const char*.

The assumption that a pointer to a character will point to a C-style string (a string ending in 0) is still unclear, which can be a source of uncertainty and error. Use czstring when referencing const char*.

/ / we can assume that p cannot be nullptr// we can assume that p points to a zero-terminated array of charactersint length (not_null p)

Note: length () is, of course, std::strlen () in disguise.

Note: length () is, of course, a disguised std::strlen.

Enforcement (implementation recommendations)

(Simple) (Foundation) If a function checks a pointer parameter against nullptr before access, on all control-flow paths, then warn it should be declared not_null.

(simple) ((basic)) if a function checks null on all control flow paths before using a pointer parameter, it issues a warning that the parameter should be declared as not_null.

(Complex) If a function with pointer return value ensures it is not nullptr on all return paths, then warn the return type should be declared not_null.

(complex) if a pointer type (return value) function confirms that the return value is not null on all its return paths, it issues a warning that the return value should be defined as not_null.

At this point, I believe you have a deeper understanding of "C++ how to use not_null to define a pointer that cannot be null". 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