473,386 Members | 2,042 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,386 software developers and data experts.

unsigned int size overflow

Hi,

In my function, I receive a value as unsigned int, and i need to check
if it is greater than a limit.
say for example,
limit = 1000;

the checking is performed like this:

if(limit< inputValue)
{
//Do this
}

In all cases this works fine, except FFFF FFFF. In this case,
limit<inputValue returns TRUE, instead of FALSE.
Is this due to overflow..what can i do for this. will a type casting
help?

Aug 17 '07 #1
12 4787
ma*********@gmail.com wrote:
>
Hi,

In my function, I receive a value as unsigned int, and i need to check
if it is greater than a limit.
say for example,
limit = 1000;

the checking is performed like this:

if(limit< inputValue)
{
//Do this
}

In all cases this works fine, except FFFF FFFF. In this case,
limit<inputValue returns TRUE, instead of FALSE.
Is this due to overflow..what can i do for this. will a type casting
help?
Make sure that (limit) is of type (unsigned int).

I'm suspecting a signed/unsigned mistmatch,
though I can't see exactly how it would work out
to give you what you are seeing.

--
pete
Aug 17 '07 #2
On Aug 17, 6:59 pm, pete <pfil...@mindspring.comwrote:
marydeep...@gmail.com wrote:
Hi,
In my function, I receive a value as unsigned int, and i need to check
if it is greater than a limit.
say for example,
limit = 1000;
the checking is performed like this:
if(limit< inputValue)
{
//Do this
}
In all cases this works fine, except FFFF FFFF. In this case,
limit<inputValue returns TRUE, instead of FALSE.
Is this due to overflow..what can i do for this. will a type casting
help?

Make sure that (limit) is of type (unsigned int).

I'm suspecting a signed/unsigned mistmatch,
though I can't see exactly how it would work out
to give you what you are seeing.

--
pete- Hide quoted text -

- Show quoted text -
Limit was unsigned..I did a typecast, though there was no type
mismatch...and it worked :))
Thanks alot :)

Aug 17 '07 #3
ma*********@gmail.com wrote:
Limit was unsigned..I did a typecast, though there was no type
mismatch...and it worked :))
This may mean that you have papered over the problem, not solved it.
Post your actual code, whittled down to an example that shows the actual
problem with complete declarations, if you want a definitive solution.

Richard
Aug 17 '07 #4
ma*********@gmail.com wrote:
>
On Aug 17, 6:59 pm, pete <pfil...@mindspring.comwrote:
marydeep...@gmail.com wrote:
Hi,
In my function, I receive a value as unsigned int, and i need to check
if it is greater than a limit.
say for example,
limit = 1000;
the checking is performed like this:
if(limit< inputValue)
{
//Do this
}
In all cases this works fine, except FFFF FFFF. In this case,
limit<inputValue returns TRUE, instead of FALSE.
Is this due to overflow..what can i do for this.
will a type casting help?
Make sure that (limit) is of type (unsigned int).

I'm suspecting a signed/unsigned mistmatch,
though I can't see exactly how it would work out
to give you what you are seeing.
Limit was unsigned..I did a typecast, though there was no type
mismatch...and it worked :))
Thanks alot :)
It's probably better to declare (limit) as unsigned int,
than to cast it.
Or if you are using a constant as (limit),
1000u would be better to use than 1000.

Casts should be used sparingly,
as they are like warning signs to someone reading code,
that something tricky is going on.

I don't think that comparing two integers is
the kind of thing that should require a cast.

--
pete
Aug 17 '07 #5
ma*********@gmail.com wrote:
Hi,

In my function, I receive a value as unsigned int, and i need to check
if it is greater than a limit.
say for example,
limit = 1000;

the checking is performed like this:

if(limit< inputValue)
{
//Do this
}

In all cases this works fine, except FFFF FFFF. In this case,
limit<inputValue returns TRUE, instead of FALSE.
Is this due to overflow..what can i do for this. will a type casting
help?
what type is your `limit'? Is it unsigned?
And try to use:
`unsigned limit = 1000U;
Aug 17 '07 #6
On Aug 17, 8:54 pm, Ivan Gotovchits <ivan.gotovch...@auriga.ruwrote:
marydeep...@gmail.com wrote:
Hi,
In my function, I receive a value as unsigned int, and i need to check
if it is greater than a limit.
say for example,
limit = 1000;
the checking is performed like this:
if(limit< inputValue)
{
//Do this
}
In all cases this works fine, except FFFF FFFF. In this case,
limit<inputValue returns TRUE, instead of FALSE.
Is this due to overflow..what can i do for this. will a type casting
help?

what type is your `limit'? Is it unsigned?
And try to use:
`unsigned limit = 1000U;- Hide quoted text -

- Show quoted text -
My code was like this
//i will get inputVal from another module.
void(unsigned int inputVal)
{
if(isLimitExceeded(inputVal))
printf("ERROR!");
}
boolean isLimitexceeded(unsigned int val)
{
unsigned long limit = 1000;
if(limit<val)
return FALSE;
else
return TRUE;
}

it worked fine on all cases except FFFF FFFF which is the boundary
condition..on this particular condition the value of val became -Ve.
Aug 17 '07 #7
ma*********@gmail.com wrote:
On Aug 17, 8:54 pm, Ivan Gotovchits <ivan.gotovch...@auriga.ruwrote:
> marydeep...@gmail.com wrote:
>>Hi,
In my function, I receive a value as unsigned int, and i need to check
if it is greater than a limit.
say for example,
limit = 1000;
the checking is performed like this:
if(limit< inputValue)
{
//Do this
}
In all cases this works fine, except FFFF FFFF. In this case,
limit<inputValue returns TRUE, instead of FALSE.
Is this due to overflow..what can i do for this. will a type casting
help?
what type is your `limit'? Is it unsigned?
And try to use:
`unsigned limit = 1000U;- Hide quoted text -

- Show quoted text -
My code was like this
//i will get inputVal from another module.
void(unsigned int inputVal)
{
if(isLimitExceeded(inputVal))
printf("ERROR!");
}
boolean isLimitexceeded(unsigned int val)
{
unsigned long limit = 1000;
if(limit<val)
return FALSE;
else
return TRUE;
}

it worked fine on all cases except FFFF FFFF which is the boundary
condition..on this particular condition the value of val became -Ve.
You're still not telling the whole tale: is it `val'
or `limit' that is 0xFFFFFFFF in the failing case? And
what is the nature of the "failure?"

Also, what evidence suggests that `val' is negative?
In the code you have shown, it is impossible for `val' to
be negative. (Is this your actual code? You say the code
was "like" this, but was it "exactly" this? One thing that
makes me suspicious is the name of the function, which seems
opposite to what the function actually does. Please post
the code for a complete, compilable, executable program that
demonstrates your problem.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Aug 17 '07 #8
ma*********@gmail.com wrote:
My code was like this
//i will get inputVal from another module.
void(unsigned int inputVal)
{
if(isLimitExceeded(inputVal))
printf("ERROR!");
}
boolean isLimitexceeded(unsigned int val)
{
unsigned long limit = 1000;
if(limit<val)
return FALSE;
else
return TRUE;
}
You've got the test the wrong way round, surely. If the limit is exceeded,
ie `val limit`, you want to return TRUE not FALSE, and similarly for
the other case.

So it should be

if (limit < val) return FALSE; else return TRUE;

which of course is unnecessarily complicated when you can write

return limit < val;
or
return val limit;

I'd prefer the latter because I think it reads better, but that's
likely because my native tongue is the subject-first English language.

That aside:
it worked fine on all cases except FFFF FFFF which is the boundary
condition..on this particular condition the value of val became -Ve.
/All/ cases? So you did try 0xFFFFFFFE and 0x80000000? How did you
know "the value of val became -Ve"? If you printed an unsigned int
out using %d, you will get bad answers for about half the range of
values ...

Show /actual code/ with the problem you had. Not code it was "like".
Complete with the tests for negativity.

--
Chris "unsigned" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Aug 17 '07 #9
Chris Dollin wrote:
ma*********@gmail.com wrote:
>My code was like this
//i will get inputVal from another module.
void(unsigned int inputVal)
{
if(isLimitExceeded(inputVal))
printf("ERROR!");
}
boolean isLimitexceeded(unsigned int val)
{
unsigned long limit = 1000;
if(limit<val)
return FALSE;
else
return TRUE;
}

You've got the test the wrong way round, surely. If the limit is exceeded,
ie `val limit`, you want to return TRUE not FALSE, and similarly for
the other case.

So it should be

if (limit < val) return FALSE; else return TRUE;
(fx:cough) Petard own hoist with. It should be

if (limit < val) return TRUE; else return FALSE;

The following was OK.
which of course is unnecessarily complicated when you can write

return limit < val;
or
return val limit;
--
Chris "crushed with his own pestle, too" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Aug 17 '07 #10
ma*********@gmail.com writes:
[...]
My code was like this
//i will get inputVal from another module.
void(unsigned int inputVal)
{
if(isLimitExceeded(inputVal))
printf("ERROR!");
}
boolean isLimitexceeded(unsigned int val)
{
unsigned long limit = 1000;
if(limit<val)
return FALSE;
else
return TRUE;
}

it worked fine on all cases except FFFF FFFF which is the boundary
condition..on this particular condition the value of val became -Ve.
Showing us what your code is *like* doesn't help us. In trimming the
code, you've trimmed away whatever is causing the problem.

Show us what your code *is*. Write a small complete program that
exhibits the problem. The program needs to compile and run (i.e.,
there needs to be a main function), and its output needs to
demonstrate the problem (you'll need '#include <stdio.h>').
Copy-and-paste the exact code.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 17 '07 #11
ma*********@gmail.com wrote:
>
.... snip ...
My code was like this
//i will get inputVal from another module.
void(unsigned int inputVal) {
if(isLimitExceeded(inputVal))
printf("ERROR!");
}
boolean isLimitexceeded(unsigned int val) {
unsigned long limit = 1000;
if(limit<val) return FALSE;
else return TRUE;
}

it worked fine on all cases except FFFF FFFF which is the boundary
condition..on this particular condition the value of val became -Ve.
No, ffffffff is the maximum value for the unsigned, and thus no
unsigned value can possibly exceed it. Check the system value for
UINTMAX in limits.h.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 17 '07 #12
ma*********@gmail.com wrote:
boolean isLimitexceeded(unsigned int val)
{
unsigned long limit = 1000;
if(limit<val)
return FALSE;
else
return TRUE;
}
No matter what type you choose,
(limit) and (val) should both be exactly the same type,
AND THEY'RE NOT!!!

--
pete
Aug 17 '07 #13

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

Similar topics

96
by: John Harrison | last post by:
I knew that unsigned integral data types were the cause of scads of mostly spurious warning messages, but I didn't realise that they were a security risk too (see here...
5
by: wallacej | last post by:
Does anybody know why unsigned char myImage; works but unsigned char myImage; does not? I think its because the second line is a value too big for unsigned char, is this ...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
5
by: Adam Warner | last post by:
Hi all! When dealing with dynamically adjusted objects are you ever concerned that you'll double the requested size of the object and overflow size_t so that the requested size e.g. becomes...
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. ...
3
by: linq936 | last post by:
I have some algorithm dealing with unsigned int calculation a lot, I am trying to add some check for overflow. The initial thinking was very easy, just do something like this, int...
3
by: subramanian100in | last post by:
Suppose unsigned int size = UINT_MAX; Now consider ++size; After increment operator, size value becomes zero in
17
by: Tarique | last post by:
This program was compiled on MS Visual C++ 08 /*Fibonacci Numbers*/ #include<stdio.h> #include<limits.h> void fibonacci(int n) { unsigned long long fib0 = 0; /*First Fibonacci Number*/
2
by: sam.barker0 | last post by:
Hi guys, I am trying to form an IPV6 address string from the address bytes contained in a unsigned char buffer char tempstring; sprintf(tempstring, "%x:%x:%x:%x:%x:%x:%x:%x",htons(*((unsigned...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.