0

When I run this function, It gives me a 2 warning for setw(*torPtr - *harePtr) and setw(*harePtr - *torPtr)

It said :

Arithmetic overflow: Using operator '-' on a 4-byte value and then casting the result to an 8-byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).

How can I fix this please?

void Posi(const int* const tPtr,const int* const hPtr)
{
    if (*hPtr == *tPtr) {
        cout <<setw(*hPtr) << "bang!" << '\a';  
    }
    else if (*hPtr < *tPtr) {
        cout << setw(*hPtr) << 'H' << setw(*tPtr - *hPtr) << 'A';
    }
    else {
        cout  << setw(*tPtr) << 'T' << setw(*hPtr - *tPtr) << 'B';
    }
}
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
carol
  • 5
  • 2
  • I can't reproduce, everything looks ok https://wandbox.org/permlink/jfz1q5uZoUKoDcdS, the problem must be elsewere, post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – anastaciu Apr 16 '20 at 22:51
  • Wow, the error message complains about "casting" when there are no casts? The term here should be "converting". Compiler writers should know the difference. – Pete Becker Apr 17 '20 at 14:40
  • Duplicate of https://stackoverflow.com/questions/59308422/arithmetic-overflow-using-operator-on-a-4-byte-value-then-casting-the-resul ? – MSalters Apr 17 '20 at 14:42

1 Answers1

1

When using Visual Studio, I get this error as well. After looking into setw that I linked from #include <iomanip>, I found that setw is provided with a parameter streamsize which is actually a long long.

The resulting problem seems to be that you are trying to cast the arithmetic result of two int (with a size of 4 bytes) to a long long (with a size of 8 bytes) to conform to the definition of streamsize in setw.

An overflow caused by an arithmetic operation would not yield correct results. If you want to learn, how an overflow is caused, you could look at the following web-article https://www.cplusplus.com/articles/DE18T05o/.

To fix the problem, you will need to prevent the overflow from occuring, which can be achieved by casting the values to a larger data type. For example:

const long long value_cast = static_cast<long long>(*tPtr) - static_cast<long long>(*hPtr);
cout << setw(*hPtr) << 'H' << setw(value_cast) << 'A';

I hope that this answers your question. :)

Edit: I changed the cast from c-style to static. Thank you for your contribution anastaciu!

Erik Wessel
  • 501
  • 3
  • 8
  • 2
    Well spotted, that seems to be it, since this is C++ it would be best to use C++ style cast `static_cast(*tPtr)`, etc. – anastaciu Apr 16 '20 at 23:52
  • The answer is working. Thank you very much for your help – carol Apr 16 '20 at 23:57
  • 1
    FYI: despite the (erroneous) use of the word "casting" in the error message, there are no casts in the original code. There are **conversions**, and that's what the compiler should be complaining about. A cast is something you write in your source code to tell the compiler to do a conversion. +1. – Pete Becker Apr 17 '20 at 14:42