To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
Type Conversion in C++ and C# Arithmeti…

Type Conversion in C++ and C# Arithmetic Expressions

Mar 29 2016
Author:

In arithmetic expressions, the types of operands can be converted to a common type. Such conversions are described in the language standard, and in C# they are much simpler than in C++. However, I'm not sure that many programmers know all the details.

Perhaps you had situations when the type of an arithmetic expression turned out to be something different from what you had expected. How well do you know the language standard? Test yourself by replacing auto and var with appropriate types in the expressions below and evaluating these expressions:

C++ (we're assuming that an LP64 data model is used):

void Test()
{
    unsigned char c1 = std::numeric_limits<unsigned char>::max();
    unsigned char c2 = std::numeric_limits<unsigned char>::max();
    int i1 = std::numeric_limits<int>::max();
    int i2 = std::numeric_limits<int>::max();
    unsigned int u1 = std::numeric_limits<unsigned int>::max();

    auto x = c1 + c2;
    auto y = i1 + i2;
    auto z = i1 + u1;
}

C#:

void Test()
{
    byte b1 = byte.MaxValue;
    byte b2 = byte.MaxValue;
    int i1 = int.MaxValue;
    int i2 = int.MaxValue;
    uint u1 = uint.MaxValue;

    var x = b1 + b2;
    var y = i1 + i2;
    var z = i1 + u1;
}

The answer is below the picture

0386_TypeConversion/image1.png

C++ (LP64):

    int x = c1 + c2;          // = 510
    int y = i1 + i2;          // = -2
    unsigned int z = i1 + u1; // = 2147483646

C#:

    int x = b1 + b2;          // = 510
    int y = i1 + i2;          // = -2
    long z = i1 + u1;         // = 6442450942

Here is what follows from this test - or rather the C++ and C# standards:

1. Evaluation of x. In an arithmetic expression, all the variables whose values can be represented with type int will be converted to this type, so when adding two variables of type char, unsigned char, short int, or unsigned short int in C++, or variables of type byte, sbyte, short, or ushort in C#, the resulting value will be of type int and no overflow will occur. In our examples, the x variable will take the value 510.

2. Evaluation of y. If both arguments are of type int, no further type promotion will take place and an overflow is possible. In C++, an overflow leads to undefined behavior. In C#, the application will continue running by default. You can use the checked keyword or /checked compiler switch to change its behavior so that it raises an OverflowException in the case of an overflow. In our test, the y variable will take the value -2 both in C++ and C#. However, remember that in C++ we'll be dealing with undefined behavior, which may manifest itself in any way - for example writing the number 100500 to y or ending up with a stack overflow.

3. Evaluation of z. The situation when one of the arguments is of type int and the other is of type unsigned int in C++ or uint in C# is handled differently by each standard! In C++, both arguments will be converted to type unsigned int. By the way, if an overflow occurs, it wouldn't be an undefined behavior. In C#, both arguments will be converted to type long and no overflow will be ever possible. It is the reason why we got different values for the z variable in our programs in different languages.

Now let's see what errors can be found in code written without taking type-conversion rules into account.

C++ example:

typedef unsigned int    Ipp32u;
typedef signed int      Ipp32s;

Ipp32u m_iCurrMBIndex;

VC1EncoderMBInfo* VC1EncoderMBs::GetPevMBInfo(Ipp32s x, Ipp32s y)
{
    Ipp32s row = (y > 0) ? m_iPrevRowIndex : m_iCurrRowIndex;
    return ((m_iCurrMBIndex - x < 0 || row < 0)
        ? 0 : &m_MBInfo[row][m_iCurrMBIndex - x]);
}

This code fragment is taken from IPP Samples project. When comparing an expression result with zero, one should keep in mind that int may be cast to unsigned int, and long to unsigned long. In our case, the result of the m_iCurrMBIndex - x expression will be of type unsigned int, so it is always nonnegative - PVS-Studio will warn you about this issue: V547 Expression 'm_iCurrMBIndex - x < 0' is always false. Unsigned type value is never < 0.

C# example:

public int Next(int minValue, int maxValue)
{
    long num = maxValue - minValue;
    if (num <= 0x7fffffffL)
    {
        return (((int)(this.Sample() * num)) + minValue);
    }
    return (((int)((long)(this.GetSampleForLargeRange() * num)))
        + minValue);
}

This sample is taken from SpaceEngineers project. In C#, you should always keep in mind that when adding two variables of type int, their type will never be promoted to long, unlike the situation when you add a variable of type int and a variable of type uint. Therefore, what will be written to the num variable is an int value, which always meets the num <= 0x7fffffffL condition. PVS-Studio knows about this issue and generates the message V3022 Expression 'num <= 0x7fffffffL' is always true.

It's great when you know the standard and know how to avoid errors like those discussed above, but in real life remembering all the intricacies of language is difficult - and totally impossible in the case of C++. And here's where static analyzers like PVS-Studio may be of help.

References

Popular related articles
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…

Comments (0)

Next comments

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept