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 to avoid lossy arithmetic conversion in C++

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

Share

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

This article introduces the knowledge of "how to avoid harmful arithmetic conversion on C++". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

ES.46: avoid lossy (narrowing, truncating) arithmetic conversion

Reason (reason)

A narrowing conversion destroys information, often unexpectedly so.

Narrowing conversion destroys information, usually not an expected action.

Example, bad (negative example)

A key example is basic narrowing:

The main examples illustrate the basics of narrowing:

Double d = 7.9

Int I = d; / / bad: narrowing: I becomes 7

I = (int) d; / / bad: we're going to claim this is still not explicit enough

Void f (int x, long y, double d)

{

Char C1 = x; / / bad: narrowing

Char c2 = y; / / bad: narrowing

Char c3 = d; / / bad: narrowing

}

Note (Note)

The guidelines support library provides a narrow_cast operation that can be used to indicate that narrowing is acceptable; a narrow ("if narrowing conversion occurs") operation that throws an exception when any information is lost.

I = narrow_cast (d); / / OK (you asked for it): narrowing: I becomes 7

I = narrow (d); / / OK: throws narrowing_error

We also include lossy arithmetic casts, such as from a negative floating point type to an unsigned integral type:

These two operations can also handle lossy arithmetic conversions, such as conversions from negative floating point numbers to unsigned integers.

Double d =-7.9

Unsigned u = 0

U = d; / / BAD

U = narrow_cast (d); / / OK (you asked for it): U becomes 4294967289

U = narrow (d); / / OK: throws narrowing_errorEnforcement (implementation recommendation)

A well-implemented code analyzer can detect all narrowing transformations. However, identifying all narrowing transformations can lead to a large number of false positive results. Recommendations:

Flag all floating-point to integer conversions (maybe only float- > char and double- > int. Here be dragons! We need data).

Mark all floating point to integer conversions (or just float to char and double to int. It's possible! We need data)

Flag all long- > char (I suspect int- > char is very common. Here be dragons! We need data).

Mark all long-to-char conversions (I suspect int-to-char conversions are common. It's possible! We need data)

Consider narrowing conversions for function arguments especially suspect.

Narrowing conversions of function parameters are particularly suspicious.

This is the end of the content of "how to avoid harmful arithmetic conversion on C++". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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