Board index » cppbuilder » casting a constant

casting a constant


2006-07-14 12:56:07 AM
cppbuilder0
In BDS2006, I am getting a W8056 Integer arithmetic overflow warning for the
following:
ULARGE_INTEGER TotalNumberOfBytes;
if(TotalNumberOfBytes.QuadPart == 8430829568)
;;
The code works fine, but was wondering how I can cast the numeric constant
to avoid the warning.
(UINT64) doesn't work, nor does (ULARGE_INTEGER.QuadPart)
Thanks
Kevin
 
 

Re:casting a constant

"Kevin" < XXXX@XXXXX.COM >wrote in message
Quote
if(TotalNumberOfBytes.QuadPart == 8430829568)
if( TotalNumberOfBytes.QuadPart == 8430829568i64 )
Notice the 'i64' suffix.
Gambit
 

Re:casting a constant

"Kevin" < XXXX@XXXXX.COM >writes:
Quote
In BDS2006, I am getting a W8056 Integer arithmetic overflow warning for the
following:

ULARGE_INTEGER TotalNumberOfBytes;

if(TotalNumberOfBytes.QuadPart == 8430829568)
;;

The code works fine, but was wondering how I can cast the numeric constant
to avoid the warning.

(UINT64) doesn't work, nor does (ULARGE_INTEGER.QuadPart)
When you cast it (you're NOT actually using c-style casts, right?),
the problem is that the large literal number has already overflowed.
The standard allows the compiler to represent the literal in a type
that holds it, up to unsigned long. If its bigger than that you get
undefined behavior.
However, if you explicitly state the type, then it cam work. If I
recall correctly, the way to mark a number as being 64-bits in BCB is
to use i64 and ui64 suffixes:
if (TotalNumberOfBytes.QuadPart == 8430829568ui64)
On g++, it's ull (unsigned long long), and that'll probably
eventually become the standard, once "long long" is part of standard
C++.
I hope this works for you, as the suggestion is untested.
--
Chris (TeamB);
 

{smallsort}

Re:casting a constant

Quote
When you cast it (you're NOT actually using c-style casts, right?),
the problem is that the large literal number has already overflowed.

The standard allows the compiler to represent the literal in a type
that holds it, up to unsigned long. If its bigger than that you get
undefined behavior.

However, if you explicitly state the type, then it cam work. If I
recall correctly, the way to mark a number as being 64-bits in BCB is
to use i64 and ui64 suffixes:

if (TotalNumberOfBytes.QuadPart == 8430829568ui64)
Thanks to Remy and Chris for the help, and the tutorial. Warning is gone.
Re: the large literal being overflowed. This makes sense (now), but it
makes me wonder why the code worked at all since the literals in the app are
Quote
unsigned long. There was no warning from the BCB5 compiler (maybe I had
them turned off?), and I was not using any casts. The warning only appeared
when converting to the BDS compiler.
And, I 'fess that I was using c-style casts --- but only for testing ;-)
K