LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2010, 02:56 PM   #1
nitin_ucer
LQ Newbie
 
Registered: Aug 2007
Posts: 14

Rep: Reputation: 0
Integral constant overflow


Following piece of code is giving "Integral constant overflow" Error :

#define MAXSIZE (512*1024*1024*1024)
UINT64 size;
If(size > MAXSIZE)
{
// do something
}

UINT64 can have values ranging from 0 to 18,446,744,073,709,551,615.

Then why i am getting this compiler error ? How to handle this ?
 
Old 09-10-2010, 02:59 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
The macro expands to:

Code:
if (size > (512*1024*1024*1024))
Then when the right-hand side is evaluated, a temporary variable is automatically created to store the result, before it is compared with size. That variable is a 32-bit int.

Last edited by dugan; 09-10-2010 at 03:13 PM.
 
Old 09-10-2010, 03:28 PM   #3
nitin_ucer
LQ Newbie
 
Registered: Aug 2007
Posts: 14

Original Poster
Rep: Reputation: 0
I defined MAXSIZE as below and it worked

#define MAXSIZE 549755813888 // actual value of (512*1024*1024*1024)

thanks !!!!

Is there any other way to handle this ? putting a big number in Macro doesn't look good.
 
Old 09-10-2010, 05:37 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by nitin_ucer View Post
I defined MAXSIZE as below and it worked

#define MAXSIZE 549755813888 // actual value of (512*1024*1024*1024)

thanks !!!!

Is there any other way to handle this ? putting a big number in Macro doesn't look good.
Yes, there is - add (IIRC) 'ul' suffix to the numbers to be multiplied. Better yet check http://www.open-std.org/JTC1/SC22/wg...docs/n1124.pdf - C99 standard.
 
1 members found this post helpful.
Old 09-10-2010, 05:46 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by nitin_ucer View Post
Is there any other way to handle this ? putting a big number in Macro doesn't look good.
I'm curious as to why you think so, considering that the big-number-constant version executes faster.
 
Old 09-12-2010, 11:15 AM   #6
nitin_ucer
LQ Newbie
 
Registered: Aug 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by dugan View Post
I'm curious as to why you think so, considering that the big-number-constant version executes faster.
I do agree with you.MAXSIZE is representing 512GB , so for readability I wanted to put it as 512*1024*1024*1024.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
No sound in Ubuntu Studio 7.10! integral feature! jamwaffles Linux - Hardware 5 04-10-2008 10:44 AM
What is integral (x tan(x) dx) corbis_demon Linux - General 1 12-06-2007 05:29 AM
help with my C program, switch expression not integral? NetBlaster Programming 2 05-05-2006 02:39 PM
Integral IC chips in inkjet printers xuper Linux - Hardware 3 06-13-2005 01:01 AM
Integral 1GB USB 2.0 Flash Drive GenericProdigy Linux - Hardware 0 12-16-2004 04:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration