Casting (signed long)(unsigned long)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • curious_one

    Casting (signed long)(unsigned long)

    All,
    I have a sample code that does something like this,

    unsigned long i = SOME_HUGE_VALUE ;
    unsigned long j = SOME_HUGE_VALUE ;

    void decrement_count ()
    {
    if((signed long)j > 0)
    j--
    else
    printf("Empty") ;
    }

    void add_count()
    {
    if((signed long)j < i)
    {
    j++;
    }
    else
    {
    printf("Error") ;
    }
    }

    Will the casting in add_count truncate the j value while
    comparision,as a result j++ will have a value more than i at some
    point?

    regds
    curious
  • Dan Pop

    #2
    Re: Casting (signed long)(unsigned long)

    In <dee72238.04012 92201.486a36ec@ posting.google. com> curious.one@eud oramail.com (curious_one) writes:
    [color=blue]
    > I have a sample code that does something like this,
    >
    > unsigned long i = SOME_HUGE_VALUE ;
    > unsigned long j = SOME_HUGE_VALUE ;
    >
    > void decrement_count ()
    >{
    > if((signed long)j > 0)
    > j--
    > else
    > printf("Empty") ;
    >}
    >
    >void add_count()
    >{
    > if((signed long)j < i)
    > {
    > j++;
    > }
    > else
    > {
    > printf("Error") ;
    > }
    >}[/color]

    Most likely, this code was written by someone who didn't know what he
    was doing. To behave correctly, both casts must be eliminated.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email: Dan.Pop@ifh.de

    Comment

    • Jack Klein

      #3
      Re: Casting (signed long)(unsigned long)

      On 29 Jan 2004 22:01:17 -0800, curious.one@eud oramail.com
      (curious_one) wrote in comp.lang.c:
      [color=blue]
      > All,
      > I have a sample code that does something like this,
      >
      > unsigned long i = SOME_HUGE_VALUE ;
      > unsigned long j = SOME_HUGE_VALUE ;
      >
      > void decrement_count ()
      > {
      > if((signed long)j > 0)[/color]

      This may NEVER be true. An unsigned long with a value greater than
      ULONG_MAX will result in a negative value when cast to signed long on
      most implementations .
      [color=blue]
      > j--
      > else
      > printf("Empty") ;
      > }
      >
      > void add_count()
      > {
      > if((signed long)j < i)
      > {
      > j++;
      > }
      > else
      > {
      > printf("Error") ;
      > }
      > }
      >
      > Will the casting in add_count truncate the j value while
      > comparision,as a result j++ will have a value more than i at some
      > point?[/color]

      No, there is no such guarantee.

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      • Peter Nilsson

        #4
        Re: Casting (signed long)(unsigned long)

        "Jack Klein" <jackklein@spam cop.net> wrote in message
        news:m88m10dbvu uuv31gp83efkj2t aq79ict90@4ax.c om...[color=blue]
        > On 29 Jan 2004 22:01:17 -0800, curious.one@eud oramail.com
        > (curious_one) wrote in comp.lang.c:
        >[color=green]
        > > All,
        > > I have a sample code that does something like this,
        > >
        > > unsigned long i = SOME_HUGE_VALUE ;
        > > unsigned long j = SOME_HUGE_VALUE ;
        > >
        > > void decrement_count ()
        > > {
        > > if((signed long)j > 0)[/color]
        >
        > This may NEVER be true. An unsigned long with a value greater than
        > ULONG_MAX will result in a negative value when cast to signed long on[/color]
        ^^^^^^^^^[color=blue]
        > most implementations .[/color]

        You mean: LONG_MAX

        --
        Peter


        Comment

        • curious_one

          #5
          Re: Casting (signed long)(unsigned long)

          Dan,
          Thanks for your reply, I am not entirely clear on why the casts
          need to be eliminated. [Not that I know the reson why the casts are
          there in the first place :)], may i ask you the reason why the code
          will result in undefined behaviour ?

          thanks
          -curious

          an.Pop@cern.ch (Dan Pop) wrote in message news:<bvdh31$qb i$3@sunnews.cer n.ch>...[color=blue]
          > In <dee72238.04012 92201.486a36ec@ posting.google. com> curious.one@eud oramail.com (curious_one) writes:
          >[color=green]
          > > I have a sample code that does something like this,
          > >
          > > unsigned long i = SOME_HUGE_VALUE ;
          > > unsigned long j = SOME_HUGE_VALUE ;
          > >
          > > void decrement_count ()
          > >{
          > > if((signed long)j > 0)
          > > j--
          > > else
          > > printf("Empty") ;
          > >}
          > >
          > >void add_count()
          > >{
          > > if((signed long)j < i)
          > > {
          > > j++;
          > > }
          > > else
          > > {
          > > printf("Error") ;
          > > }
          > >}[/color]
          >
          > Most likely, this code was written by someone who didn't know what he
          > was doing. To behave correctly, both casts must be eliminated.
          >
          > Dan[/color]

          Comment

          • pete

            #6
            Re: Casting (signed long)(unsigned long)

            curious_one wrote:[color=blue]
            >
            > All,
            > I have a sample code that does something like this,
            >
            > unsigned long i = SOME_HUGE_VALUE ;
            > unsigned long j = SOME_HUGE_VALUE ;
            >
            > void decrement_count ()
            > {
            > if((signed long)j > 0)
            > j--
            > else
            > printf("Empty") ;
            > }[/color]

            void decrement_count ()
            {
            if (j != 0) j--;
            else printf("Empty") ;
            }
            [color=blue]
            >
            > void add_count()
            > {
            > if((signed long)j < i)
            > {
            > j++;
            > }
            > else
            > {
            > printf("Error") ;
            > }
            > }[/color]


            void add_count()
            {
            if (j != 0lu - 1) {
            j++;
            } else {
            printf("Error") ;
            }
            }
            [color=blue]
            > Will the casting in add_count truncate the j value while
            > comparision,as a result j++ will have a value more than i at some
            > point?[/color]

            Conversion of out of range values to signed types,
            is implementation defined.

            --
            pete

            Comment

            Working...