unsigned int size overflow

Discussion in 'C Programming' started by marydeepthy@gmail.com, Aug 17, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. pete Guest

    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
    pete, Aug 17, 2007
    #2
    1. Advertising

  3. Guest

    On Aug 17, 6:59 pm, pete <> wrote:
    > 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, 2007
    #3
  4. Richard Bos Guest

    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
    Richard Bos, Aug 17, 2007
    #4
  5. pete Guest

    wrote:
    >
    > On Aug 17, 6:59 pm, pete <> wrote:
    > > 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
    pete, Aug 17, 2007
    #5
  6. 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;
    Ivan Gotovchits, Aug 17, 2007
    #6
  7. Guest

    On Aug 17, 8:54 pm, Ivan Gotovchits <> wrote:
    > 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, 2007
    #7
  8. Eric Sosman Guest

    wrote:
    > On Aug 17, 8:54 pm, Ivan Gotovchits <> wrote:
    >> 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
    lid
    Eric Sosman, Aug 17, 2007
    #8
  9. Chris Dollin Guest

    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
    Chris Dollin, Aug 17, 2007
    #9
  10. Chris Dollin Guest

    Chris Dollin wrote:

    > 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
    Chris Dollin, Aug 17, 2007
    #10
  11. CBFalconer Guest

    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
    CBFalconer, Aug 17, 2007
    #11
  12. 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) <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"
    Keith Thompson, Aug 17, 2007
    #12
  13. pete Guest

    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
    pete, Aug 17, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timo Freiberger
    Replies:
    3
    Views:
    942
    Bob Hairgrove
    Oct 30, 2004
  2. Schnoffos
    Replies:
    2
    Views:
    1,208
    Martien Verbruggen
    Jun 27, 2003
  3. er
    Replies:
    6
    Views:
    487
    Andre Kostur
    Sep 14, 2007
  4. ciccio

    int*unsigned int = unsigned?

    ciccio, Jun 4, 2010, in forum: C++
    Replies:
    2
    Views:
    405
    Öö Tiib
    Jun 4, 2010
  5. pozz
    Replies:
    12
    Views:
    738
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page