How can I get rid of "Warning C4307: integral constant overflow"

  • Thread starter _Christopher\(M2M\)
  • Start date
C

_Christopher\(M2M\)

Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600)

How do I get rid of "Warning C4307: '-' : integral constant overflow" from
the following macro?
#define d_ReturnMax(I_iTypeSize)\
( (int64(1)<<((I_iTypeSize*8)-1))-1)

There NO warnings when done as a function:
int64 d_ReturnMax(int64 I_iTypeSize)
{ return (1<<((I_iTypeSize*8)-1))-1;}

Test code:
int64 L_iMaxIntSize=d_ReturnMax(sizeof(int32)); //
L_iMaxIntSize=0x000000007fffffff
int64 L_iMaxIntSize=d_ReturnMax(sizeof(char)); //
L_iMaxIntSize=0x000000000000007f

I tried typecasting around all parts of code i.e. int64(...)/(int64)... I
give up now.
Can anyone else spot something I have missed?

--
From _Christopher (M2M).
RefCode:44CdccTCDf42 V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject, any post without the ##71; tag WILL be deleted. I.e. "##71;
Thank for the help."
 
V

Victor Bazarov

_Christopher(M2M) said:
Microsoft Visual Studio 2005 Version 8.0.50727.762 (SP.050727-7600)

How do I get rid of "Warning C4307: '-' : integral constant overflow"
from the following macro?
#define d_ReturnMax(I_iTypeSize)\
( (int64(1)<<((I_iTypeSize*8)-1))-1)

There NO warnings when done as a function:
int64 d_ReturnMax(int64 I_iTypeSize)
{ return (1<<((I_iTypeSize*8)-1))-1;}

Test code:
int64 L_iMaxIntSize=d_ReturnMax(sizeof(int32)); //
L_iMaxIntSize=0x000000007fffffff
int64 L_iMaxIntSize=d_ReturnMax(sizeof(char)); //
L_iMaxIntSize=0x000000000000007f

I tried typecasting around all parts of code i.e.
int64(...)/(int64)... I give up now.
Can anyone else spot something I have missed?

Probably. One, 'int64' is compiler-specific and should be asked
about in the newsgroup dedicated to that compiler. Two, to form
a constant of a particular type you need to follow it with a certain
suffix, like 0xffL for 'long' or 0x55U for 'unsigned' (hint: int64
probably has its own dedicated suffix), do not use a "cast". Also,
shifts only work with the right argument between 0 and some
(relatively small) number. Make sure whatever '((I_iTypeSize*8)-1))'
(do you really need all those parentheses?) expands into is between
0 and the maximum allowed value (63?).

V
 
J

James Kanze

Probably. One, 'int64' is compiler-specific and should be asked
about in the newsgroup dedicated to that compiler.

int64_t is part of C99, and is in the current draft. It's
pretty obvious, I think, what int64 is supposed to be.
Two, to form
a constant of a particular type you need to follow it with a certain
suffix, like 0xffL for 'long' or 0x55U for 'unsigned' (hint: int64
probably has its own dedicated suffix), do not use a "cast".

There's no problem using a cast as long as the value is in fact
representable as an int. In his case, the value is 1, so
there's no problem. In many cases, the cast is necessary,
because you don't know the actual type involved, e.g. if dealing
with size_t.
Also,
shifts only work with the right argument between 0 and some
(relatively small) number.
Make sure whatever '((I_iTypeSize*8)-1))'
(do you really need all those parentheses?) expands into is between
0 and the maximum allowed value (63?).

In his test code, the conditions are met. Or at least they are
if int64 really is a 64 bit int; the only explination I can see
is that the compiler is evaluating the integral constant
expression as a 32 bit long, despite the presence of an int64 in
it. (Perhaps this part of the compiler hasn't been reworked to
reflect the presence of the new integral types.) At any rate,
there is no overflow in his expression.
 
C

_Christopher\(M2M\)

Probably. One, 'int64' is compiler-specific and should be asked
about in the newsgroup dedicated to that compiler.

int64_t is part of C99, and is in the current draft. It's
pretty obvious, I think, what int64 is supposed to be.
Two, to form
a constant of a particular type you need to follow it with a certain
suffix, like 0xffL for 'long' or 0x55U for 'unsigned' (hint: int64
probably has its own dedicated suffix), do not use a "cast".

There's no problem using a cast as long as the value is in fact
representable as an int. In his case, the value is 1, so
there's no problem. In many cases, the cast is necessary,
because you don't know the actual type involved, e.g. if dealing
with size_t.
Also,
shifts only work with the right argument between 0 and some
(relatively small) number.
Make sure whatever '((I_iTypeSize*8)-1))'
(do you really need all those parentheses?) expands into is between
0 and the maximum allowed value (63?).

In his test code, the conditions are met. Or at least they are
if int64 really is a 64 bit int; the only explination I can see
is that the compiler is evaluating the integral constant
expression as a 32 bit long, despite the presence of an int64 in
it. (Perhaps this part of the compiler hasn't been reworked to
reflect the presence of the new integral types.) At any rate,
there is no overflow in his expression.

--
James Kanze (GABI Software) email:[email protected]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


---------8<--------
Probably. One, 'int64' is compiler-specific and should be asked
about in the newsgroup dedicated to that compiler.

Sorry about not making it clear about the int64
typedef __int64 int64;

And yes you're right I repost it to:
microsoft.public.vc.language

Thanks anyway.
--
From _Christopher (M2M).
RefCode:444CdcTCDcl4 V04
void DeadEnds() {for(;;);} // :)

If replying by email please included ##71; on the subject line followed by
your subject, any post without the ##71; tag WILL be deleted. I.e. "##71;
Thank for the help."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top