473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

arithmetic overflow with enum

void f(unsigned int const x) {
};
int main(int argc, char* argv[]){
//unsigned short a=0xFFFF;
enum { a=0xFFFF };
f(a*0x10000+0xFFFF);
return 0;
}

I get an arithmetic overflow when using a as an enum. I prefer
constants to be enum values so I have a problem with this.

Fraser.
Oct 9 '05 #1
5 3830
Fraser Ross wrote:
void f(unsigned int const x) {
};
int main(int argc, char* argv[]){
//unsigned short a=0xFFFF;
enum { a=0xFFFF };
f(a*0x10000+0xFFFF);
return 0;
}

I get an arithmetic overflow when using a as an enum. I prefer
constants to be enum values so I have a problem with this.

Fraser.


C++ tends to expect programs to use constants for constant values, and
enums for enumerated types (and for which the precise value of the enum
is immaterial).

Greg

Oct 9 '05 #2
On Sun, 9 Oct 2005 15:18:45 +0100, "Fraser Ross" <f@ross.co.uk> wrote:
void f(unsigned int const x) {
};
int main(int argc, char* argv[]){
//unsigned short a=0xFFFF;
enum { a=0xFFFF };
f(a*0x10000+0xFFFF);
return 0;
}

I get an arithmetic overflow when using a as an enum. I prefer
constants to be enum values so I have a problem with this.

Fraser.


Well, it overflows ... what else did you expect? You need to cast
either a or 0x10000 to a larger type than int to avoid the overflow.
Default integer promotion uses int instead, which is too small to
accommodate the resulting value.

Try this instead (if your platform supports UINT64 natively, you can
comment out the typedef or change it to "unsigned long long" instead
of __int64):

// test_enum.cpp
typedef unsigned __int64 UINT64;

void f(UINT64 const x) {
};
int main(int argc, char* argv[]){
//unsigned short a=0xFFFF;
enum { a=0xFFFF };
f(a*((UINT64)0x10000)+0xFFFF);
return 0;
}

--
Bob Hairgrove
No**********@Home.com
Oct 9 '05 #3
Thats what I suspected was the problem. Its not necessary to cast to a
64 bit type though. unsigned int will do. In the documentation for
BCB6 in the description of 'treat enum types as ints' it says that "an
unsigned or signed short if the values of the enumeration are within the
following ranges: 0 to 4,294,967,295 or -2,147,483,648 to 2,147,483,647"
will be used for allocation. Is this a mistake with the ranges?

Fraser.
Oct 9 '05 #4
Ian
Fraser Ross wrote:
Thats what I suspected was the problem. Its not necessary to cast to a
64 bit type though. unsigned int will do. In the documentation for
BCB6 in the description of 'treat enum types as ints' it says that "an
unsigned or signed short if the values of the enumeration are within the
following ranges: 0 to 4,294,967,295 or -2,147,483,648 to 2,147,483,647"
will be used for allocation. Is this a mistake with the ranges?

Please quote!

0xFFFF*0x1FFFF will overflow a 32 bit in no matter what. You have to
use a 64 bit int.

Enums are not intended for constants. Use a const [u]int[8,16,32,64]_t
type instead.

Ian
Oct 9 '05 #5

"Ian"
0xFFFF*0x1FFFF will overflow a 32 bit in no matter what. You have to
use a 64 bit int. That isn't the arithmetic expession. Multiplication is with a higher
priority than addition. The result is the maximum value of an unsigned
32bit integer.

Enums are not intended for constants. Use a const [u]int[8,16,32,64]_t type instead.


I disagree and would say they are better. They are lvalues whereas
integers are rvalues.

Fraser.
Oct 10 '05 #6

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

Similar topics

3
by: Karthik | last post by:
Hi, I am writing this application that needs a lot of arithmetic calculations. I was wondering if C++ language specifies any way of detecting arithmetic overflows. Let us consider the following...
10
by: Massimiliano Alberti | last post by:
Are there rules on how should the C behave with the arithmetic operations? Are the overflows always ignored? And are they simply truncated? (if I have a 16-bits unsigned short ints unsigned short...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
4
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
3
by: Paul Cheetham | last post by:
Hi, I am developing a program that is doinbg time calculations using Ticks. Below is a piece of code that is causing an arithmetic overflow exception - the single line inside the try statement....
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
9
by: Mike Aubury | last post by:
Is there any standard (or even non-standard) way to detect limit overflow in arithmetic in C ? eg. /* assuming 4 byte ints.. */ int a=2147483647; int b=2147483647; int c;
1
by: thebigsquid | last post by:
hi, its me again, so this software obviously has serious problems. now i'm getting this error message when i try to access any of it! any ideas much appreciated thanks the big squid An...
10
by: Why Tea | last post by:
I understood that the CPU registers determine the size of the machine. But what is the correct way to code an arithmetic operation to avoid wrap-around when the code is to be run on both 32-bit and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.