472,791 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Integer overflow in expression

Hi,
I am using gcc, and I am a bit confused about the warning message I
got when I compiled and ran this small code snippet -

int main() {
unsigned long x = 3*1024*1024*1024; /* 3GB */
printf(" %x ", x);
}

I got the following error,

In function `main':
:4: warning: integer overflow in expression

but when I use this

int main() {
unsigned long x = 0xC0000000; /* 3GB */
printf(" %x ", x);
}

It doesn't give out any errors. Any idea as to why its warning about
integer overflow when it is well within the range of 32bits ? My gcc
-v

Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.3.3 [FreeBSD] 20031106

Vivek
Nov 14 '05 #1
7 17521

"Vivek Mohan" <vi****@phreaker.net> wrote in message

unsigned long x = 3*1024*1024*1024; /* 3GB error*/
unsigned long x = 0xC0000000; /* 3GB OK*/

The compiler treats raw decimal integers as signed ints by default.If you
tag an "lu" on the end it will be OK.
Nov 14 '05 #2
Vivek Mohan wrote:

I am using gcc, and I am a bit confused about the warning message I
got when I compiled and ran this small code snippet -

int main() {
unsigned long x = 3*1024*1024*1024; /* 3GB */
printf(" %x ", x);
}

I got the following error,

In function `main':
:4: warning: integer overflow in expression
The compiler considers the constants 3 and 1024 to be of type int. So
the result of the multiplication expression (3*1024*1024*1024) is also
int. But the result is too large to be represented as an int, so the
compiler complains of integer overflow.

You need to arrange things so that the result of the expression is an
unsigned long. Simply assigning the result to an unsigned long variable
won't do it. You should force the compiler to treat each operand in the
expression as an unsigned long, like this:

unsigned long x = 3ul * 1024ul * 1024ul * 1024ul;

Two other things: you forget to #include <stdio.h> (for printf), and the
conversion specifier in the printf statement should be %lx, not %x,
because x is an unsigned long.
but when I use this

int main() {
unsigned long x = 0xC0000000; /* 3GB */
printf(" %x ", x);
}

It doesn't give out any errors.


The compiler sees that the constant 0xC0000000 can't be represented as
an int, so it regards it as some other type--maybe unsigned long.

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #3
Vivek Mohan wrote:
Hi,
I am using gcc, and I am a bit confused about the warning message I
got when I compiled and ran this small code snippet -

int main() {
unsigned long x = 3*1024*1024*1024; /* 3GB */
printf(" %x ", x);
}

I got the following error,

In function `main':
:4: warning: integer overflow in expression


"3" and "1024" are both integers so you're calculating using integer
math. Specify one/both as a long constant.
Nov 14 '05 #4
In article <41**************************@posting.google.com >, Vivek Mohan wrote:
Hi,
I am using gcc, and I am a bit confused about the warning message I
got when I compiled and ran this small code snippet -

int main() {
unsigned long x = 3*1024*1024*1024; /* 3GB */
printf(" %x ", x);
}

I got the following error,

In function `main':
:4: warning: integer overflow in expression [-]
3 (signed) * 1024 (signed) * 1024 (signed) * 1024 (signed)
[-] but when I use this

int main() {
unsigned long x = 0xC0000000; /* 3GB */
printf(" %x ", x);
} [-]
Or this ...
3 * 1024 * 1024 * 1024u
.... or this ...
3u * 1024 * 1024 * 1024
.... or ...
It doesn't give out any errors.

[-]

Cheers,
Juergen

--
\ Real name : Juergen Heinzl \ no flames /
\ Email private : ju*****@manannan.org \ send money instead /
\ Photo gallery : www.manannan.org \ /
Nov 14 '05 #5
Vivek Mohan wrote:

Hi,
I am using gcc, and I am a bit confused about the warning message I
got when I compiled and ran this small code snippet -

int main() {
unsigned long x = 3*1024*1024*1024; /* 3GB */
printf(" %x ", x);
}

I got the following error,

In function `main':
:4: warning: integer overflow in expression


`3' is a signed int, and so are all three appearances
of `1024'. You have asked for these four signed ints to
be multiplied together, producing a product that is also
a signed int, and then for that signed int product to be
converted to unsigned long. The compiler is telling you
that the product is too large to fit in a signed int.

Here's a fix:

unsigned long x = 3u * 1024u * 1024u * 1024u;

Now all the operands are unsigned ints, and the product
is also an unsigned int. The product will not overflow a
32-bit unsigned int, so all will be well.

... maybe. It appears that your compiler uses 32 bits
for an int, but not all compilers do so. The C language
guarantees that an int has at least 16 bits, but does not
require it to be wider. If int (and hence unsigned int)
are 16 bits wide (or 18, or 24, or anything less than 32),
the product will not be "3GB" as you expect.

Here's a better fix:

unsigned long x = 3ul * 1024ul * 1024ul * 1024ul;

This time, all the operands are unsigned longs. In all C
implementations, long and unsigned long are at least 32
bits wide, possibly wider. That means that your calculation
will now behave as you intended, regardless of the platform.

By the way, you should use "%lx" rather than just "%x"
to print the value; the 'l' tells printf() that the operand
is "long" instead of "plain." You mention that you're using
gcc; if you run it as "gcc -Wall -W" it will catch this error
for you. (In my own work, I also add "-ansi -pedantic" for
all modules that can tolerate it; some system-dependent
modules cannot.)

--
Er*********@sun.com
Nov 14 '05 #6
Vivek Mohan wrote:
Hi,
I am using gcc, and I am a bit confused about the warning message I
got when I compiled and ran this small code snippet -

int main() {
unsigned long x = 3*1024*1024*1024; /* 3GB */
printf(" %x ", x);
}

I got the following error,
You should get more.
In function `main':
:4: warning: integer overflow in expression


The expression on the right of the equal sign has no longs (not to
mention unsigned longs), so is computed as an int. Try this fixed
version of your code:

#include <stdio.h> /* mha: fixed omitted header, providing
prototype required for variatic
function printf */
int main()
{
unsigned long x = 3ul * 1024 * 1024 * 1024; /* mha: fixed
initializer */
printf(" %lx\n", x); /* mha: fixed wrong specifier; fixed
implementation-specic behavior for
last output line not terminated with
an end-of-line character */
return 0; /* mha: added for the almost 100% of
people with C89, not C99 compilers */
}

Nov 14 '05 #7
Russell Hanneken <rg********@pobox.com> wrote:
Vivek Mohan wrote:
int main() {
unsigned long x = 3*1024*1024*1024; /* 3GB */
printf(" %x ", x);
}
You need to arrange things so that the result of the expression is an
unsigned long. Simply assigning the result to an unsigned long variable
won't do it.
The reason for this, BTW, is that by the time the assignment is done,
the computation has already been performed using ints.
You should force the compiler to treat each operand in the
expression as an unsigned long, like this:

unsigned long x = 3ul * 1024ul * 1024ul * 1024ul;


No need. All you need is to force it to consider the first operand to be
an unsigned long; automatic promotions will ensure that the rest will be
promoted before being multiplied.

Richard
Nov 14 '05 #8

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

Similar topics

20
by: Russell Shaw | last post by:
Hi, I'm using gcc-3.4.3 on a linux pc. The ints and long ints are 32 bits and long long ints are 64 bits. When i have: int num=9600; long long int reg=(long long)800000000000/(5000000*num);...
25
by: bruce.james.lee | last post by:
hi i have a problem with integer subtraction in C. printf("%d", c < (a - b)); a is got from a #define and is 0x80000000 and b is got from input and is also 0x80000000. c is ffffffff (-1). Now,...
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
33
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
40
by: Robert Seacord | last post by:
The CERT/CC has released a beta version of a secure integer library for the C Programming Language. The library is available for download from the CERT/CC Secure Coding Initiative web page at:...
13
by: Freaker85 | last post by:
Hello, I am new at programming in C and I am searching a manner to parse a string into an integer. I know how to do it in Java, but that doesn't work in C ;o) I searched the internet but I...
1
by: charles_gero | last post by:
Hi all, I had a question about the topics in the subject and posted to comp.std.c, but feel it may also be appropriate here. Please excuse this crosspost if it is in bad form. I have a...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.