unsigned int size overflow

M

marydeepthy

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?
 
P

pete

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

marydeepthy

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 :)
 
R

Richard Bos

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
 
P

pete

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

Ivan Gotovchits

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;
 
M

marydeepthy

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

Eric Sosman

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.)
 
C

Chris Dollin

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

Chris Dollin

Chris said:
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.
 
C

CBFalconer

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

Keith Thompson

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

pete

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!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top