473,468 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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."

Jun 5 '07 #1
3 9420
_Christopher(M2M) wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 5 '07 #2
On Jun 5, 10:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
_Christopher(M2M) wrote:
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.
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:ja*********@gmail.com
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

Jun 6 '07 #3
"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Jun 5, 10:49 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
_Christopher(M2M) wrote:
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.
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:ja*********@gmail.com
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."
Jun 8 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
1
by: Claire | last post by:
"Comparison to integral constant is useless; the constant is outside the range of type 'int'" How can I fix this compiler warning, please, for the following pseudocode? int m_nError; m_nError =...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.