negative number evaluating greater than string.size()

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

    negative number evaluating greater than string.size()

    Hi, below is example code which demonstrates a problem I have encountered.
    When passing a number to a function I compare it with a string's size and
    then take certain actions, unfortunately during the testing of it I
    discovered that negative numbers were being treated as if they were > 0. I
    compiled the following on mingw compiler/dev c++/windows xp.

    If I replace string.size() for a ordinary number it behaves as expected? I
    notice string.size() returns size type and not an int but how do I deal with
    that?

    Thanks in advance for any help

    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;

    void something(int);

    int main()
    {
    something(-1);
    system("PAUSE") ;
    return 0;
    }

    void something(int number) {
    string str = "sdfjksdflksdjf kjsklfjsklfj";
    if(number < str.size()) { cout << " hello" << endl; }
    else { cout << " eek" << endl; }
    }


  • Gianni Mariani

    #2
    Re: negative number evaluating greater than string.size()

    Jason wrote:[color=blue]
    > Hi, below is example code which demonstrates a problem I have encountered.
    > When passing a number to a function I compare it with a string's size and
    > then take certain actions, unfortunately during the testing of it I
    > discovered that negative numbers were being treated as if they were > 0. I
    > compiled the following on mingw compiler/dev c++/windows xp.
    >
    > If I replace string.size() for a ordinary number it behaves as expected? I
    > notice string.size() returns size type and not an int but how do I deal with
    > that?
    >
    > Thanks in advance for any help
    >[/color]

    GCC sez:

    signed_probs.cp p: In function `void something(int)' :
    signed_probs.cp p:17: warning: comparison between signed and unsigned
    integer expressions

    Changing line 17 to :

    if(number < static_cast<int >(str.size()) ) { cout << " hello" << endl; }

    solves the problem.


    Comment

    • John Harrison

      #3
      Re: negative number evaluating greater than string.size()


      "Jason" <jason.carney1@ btinternet.com> wrote in message
      news:c12qn6$ik2 $1@titan.btinte rnet.com...[color=blue]
      > Hi, below is example code which demonstrates a problem I have encountered.
      > When passing a number to a function I compare it with a string's size and
      > then take certain actions, unfortunately during the testing of it I
      > discovered that negative numbers were being treated as if they were > 0.[/color]
      I[color=blue]
      > compiled the following on mingw compiler/dev c++/windows xp.
      >
      > If I replace string.size() for a ordinary number it behaves as expected?[/color]
      I[color=blue]
      > notice string.size() returns size type and not an int but how do I deal[/color]
      with[color=blue]
      > that?
      >[/color]

      Suggest you read the recent thread 'time to get rid of unsigned?', started
      on 17/02/04.

      john


      Comment

      • John Harrison

        #4
        Re: negative number evaluating greater than string.size()


        "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
        news:c12sm0$kkk @dispatch.conce ntric.net...[color=blue]
        > Jason wrote:[color=green]
        > > Hi, below is example code which demonstrates a problem I have[/color][/color]
        encountered.[color=blue][color=green]
        > > When passing a number to a function I compare it with a string's size[/color][/color]
        and[color=blue][color=green]
        > > then take certain actions, unfortunately during the testing of it I
        > > discovered that negative numbers were being treated as if they were > 0.[/color][/color]
        I[color=blue][color=green]
        > > compiled the following on mingw compiler/dev c++/windows xp.
        > >
        > > If I replace string.size() for a ordinary number it behaves as expected?[/color][/color]
        I[color=blue][color=green]
        > > notice string.size() returns size type and not an int but how do I deal[/color][/color]
        with[color=blue][color=green]
        > > that?
        > >
        > > Thanks in advance for any help
        > >[/color]
        >
        > GCC sez:
        >
        > signed_probs.cp p: In function `void something(int)' :
        > signed_probs.cp p:17: warning: comparison between signed and unsigned
        > integer expressions
        >
        > Changing line 17 to :
        >
        > if(number < static_cast<int >(str.size()) ) { cout << " hello" << endl; }
        >
        > solves the problem.
        >[/color]

        Of course this is what the OP should do, but notice that the solution
        effectively rules out strings where size() > INT_MAX. So why design for such
        strings in the first place? Why not have size() return an int?

        john



        Comment

        • lilburne

          #5
          Re: negative number evaluating greater than string.size()

          John Harrison wrote:
          [color=blue]
          > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
          > news:c12sm0$kkk @dispatch.conce ntric.net...
          >
          >signed_probs.c pp: In function `void something(int)' :[color=green]
          >>signed_probs. cpp:17: warning: comparison between signed and unsigned
          >>integer expressions
          >>
          >>Changing line 17 to :
          >>
          >> if(number < static_cast<int >(str.size()) ) { cout << " hello" << endl; }
          >>
          >>solves the problem.
          >>[/color]
          >
          >
          > Of course this is what the OP should do, but notice that the solution
          > effectively rules out strings where size() > INT_MAX. So why design for such
          > strings in the first place? Why not have size() return an int?
          >[/color]

          Because its more flexible to have a type where someone can
          hold a string thats over 2Gb in size. I remember something
          like this being used as a justification for making size_t
          unsigned some 15 years back, either in GCC documentation or
          the "Standard C library", in relation to malloc().

          Naturally someone will tell you that you should be coding
          everything that deals with sizes and lengths with size_t.


          Comment

          • John Harrison

            #6
            Re: negative number evaluating greater than string.size()


            "lilburne" <lilburne@godzi lla.net> wrote in message
            news:c136e9$1de 8mh$1@ID-179504.news.uni-berlin.de...[color=blue]
            > John Harrison wrote:
            >[color=green]
            > > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
            > > news:c12sm0$kkk @dispatch.conce ntric.net...
            > >
            > >signed_probs.c pp: In function `void something(int)' :[color=darkred]
            > >>signed_probs. cpp:17: warning: comparison between signed and unsigned
            > >>integer expressions
            > >>
            > >>Changing line 17 to :
            > >>
            > >> if(number < static_cast<int >(str.size()) ) { cout << " hello" <<[/color][/color][/color]
            endl; }[color=blue][color=green][color=darkred]
            > >>
            > >>solves the problem.
            > >>[/color]
            > >
            > >
            > > Of course this is what the OP should do, but notice that the solution
            > > effectively rules out strings where size() > INT_MAX. So why design for[/color][/color]
            such[color=blue][color=green]
            > > strings in the first place? Why not have size() return an int?
            > >[/color]
            >
            > Because its more flexible to have a type where someone can
            > hold a string thats over 2Gb in size. I remember something
            > like this being used as a justification for making size_t
            > unsigned some 15 years back, either in GCC documentation or
            > the "Standard C library", in relation to malloc().[/color]

            Interestingly the following code

            int main()
            {
            std::string s;
            std::cout << std::hex << s.max_size() << '\n';
            }

            when compiled with gcc 3.3.1 prints

            3ffffffc

            I don't think its a serious limitation to restrict yourself to strings which
            occupy less than half of the available memory.
            [color=blue]
            >
            > Naturally someone will tell you that you should be coding
            > everything that deals with sizes and lengths with size_t.
            >[/color]

            But what happens when you need to subtract one size from another? What type
            should be used to hold the type of that operation? I don't think there is a
            good answer.

            john


            Comment

            • John Harrison

              #7
              Re: negative number evaluating greater than string.size()

              > What type should be used to hold the type of that operation?

              I meant

              What type should be used to hold the result of that operation?


              Comment

              • Rolf Magnus

                #8
                Re: negative number evaluating greater than string.size()

                lilburne wrote:
                [color=blue][color=green]
                >> Of course this is what the OP should do, but notice that the solution
                >> effectively rules out strings where size() > INT_MAX. So why design
                >> for such strings in the first place? Why not have size() return an
                >> int?
                >>[/color]
                >
                > Because its more flexible to have a type where someone can
                > hold a string thats over 2Gb in size.[/color]

                I think it's not. You'd run into troubles if you need to calculate
                offsets between two characters in your string. That offset might be
                negative. Now you can't represent all possible offests anymore, no
                matter whether you make the type for that offset signed or unsigned.
                And further, if you mix signed and unsigned in your calculations, you
                have to be very careful.

                Comment

                • Andre Kostur

                  #9
                  Re: negative number evaluating greater than string.size()

                  "John Harrison" <john_andronicu s@hotmail.com> wrote in news:c13cvl$1ds kq6$1
                  @ID-196037.news.uni-berlin.de:
                  [color=blue][color=green]
                  >> What type should be used to hold the type of that operation?[/color]
                  >
                  > I meant
                  >
                  > What type should be used to hold the result of that operation?[/color]

                  size_t, assuming the programmer has done the sane thing and checked that
                  they are actually subtracting the smaller from the larger.

                  Comment

                  • Andrey Tarasevich

                    #10
                    Re: negative number evaluating greater than string.size()

                    John Harrison wrote:[color=blue][color=green]
                    >> ...
                    >> Changing line 17 to :
                    >>
                    >> if(number < static_cast<int >(str.size()) ) { cout << " hello" << endl; }
                    >>
                    >> solves the problem.
                    >>[/color]
                    >
                    > Of course this is what the OP should do, but notice that the solution
                    > effectively rules out strings where size() > INT_MAX. So why design for such
                    > strings in the first place? Why not have size() return an int?
                    > ...[/color]

                    In my opinion, using a signed type to store an unsigned quantity is a
                    low-level design error. For this reason, in many practical applications
                    signed types in C/C++ are much less useful than unsigned types. And
                    'size()' must return an unsigned value.

                    As for correcting the OP's code, I would suggest doing it differently

                    if (number < 0 || (std::string::s ize_type) number < str.size())
                    { cout << " hello" << endl; }

                    --
                    Best regards,
                    Andrey Tarasevich

                    Comment

                    • lilburne

                      #11
                      Re: negative number evaluating greater than string.size()

                      Rolf Magnus wrote:
                      [color=blue]
                      > lilburne wrote:
                      >
                      >[color=green][color=darkred]
                      >>>Of course this is what the OP should do, but notice that the solution
                      >>>effectivel y rules out strings where size() > INT_MAX. So why design
                      >>>for such strings in the first place? Why not have size() return an
                      >>>int?
                      >>>[/color]
                      >>
                      >>Because its more flexible to have a type where someone can
                      >>hold a string thats over 2Gb in size.[/color]
                      >
                      >
                      > I think it's not.[/color]


                      I think so too.

                      [color=blue]
                      > You'd run into troubles if you need to calculate
                      > offsets between two characters in your string. That offset might be
                      > negative. Now you can't represent all possible offests anymore, no
                      > matter whether you make the type for that offset signed or unsigned.
                      > And further, if you mix signed and unsigned in your calculations, you
                      > have to be very careful.
                      >[/color]

                      I think it was for the reasons of inadvertantly mixing
                      signed and unsigned that Lakos recommended avoiding unsigned
                      in interfaces in "Large Scale C++".

                      Comment

                      • John Harrison

                        #12
                        Re: negative number evaluating greater than string.size()


                        "Andre Kostur" <nntpspam@kostu r.net> wrote in message
                        news:Xns94949E1 E88AFEnntpspamk osturnet@207.35 .177.135...[color=blue]
                        > "John Harrison" <john_andronicu s@hotmail.com> wrote in[/color]
                        news:c13cvl$1ds kq6$1[color=blue]
                        > @ID-196037.news.uni-berlin.de:
                        >[color=green][color=darkred]
                        > >> What type should be used to hold the type of that operation?[/color]
                        > >
                        > > I meant
                        > >
                        > > What type should be used to hold the result of that operation?[/color]
                        >
                        > size_t, assuming the programmer has done the sane thing and checked that
                        > they are actually subtracting the smaller from the larger.[/color]

                        It gets very tedious after a while.

                        size_t size_a = ...;
                        size_t size_b = ...;
                        size diff;
                        bool a_is_bigger;
                        if (a > b)
                        {
                        diff = size_a - size_b;
                        a_is_bigger = true;
                        }
                        else
                        {
                        diff = size_b - size_a;
                        a_is_bigger = false;
                        }
                        process_diff(di ff, a_is_bigger);

                        and hence its prone to errors, as we have all seen. In fact I very rarely
                        see code that takes this much trouble. It would be easier if size_t was
                        defined as a signed integer, and not a great deal would have been lost.

                        john



                        Comment

                        • Andrey Tarasevich

                          #13
                          Re: negative number evaluating greater than string.size()

                          John Harrison wrote:[color=blue]
                          > ...
                          > It would be easier if size_t was
                          > defined as a signed integer, and not a great deal would have been lost.
                          > ...[/color]

                          It wouldn't solve anything. It would just made the problem less obvious.
                          In general case the difference between two signed integers does not fit
                          into the range of the same signed integer type. The resultant signed
                          type must be at least one bit longer than original types.

                          --
                          Best regards,
                          Andrey Tarasevich

                          Comment

                          • John Harrison

                            #14
                            Re: negative number evaluating greater than string.size()


                            "Andrey Tarasevich" <andreytarasevi ch@hotmail.com> wrote in message
                            news:FtWdnWvGhP 0GJajdRVn-jg@comcast.com. ..[color=blue]
                            > John Harrison wrote:[color=green]
                            > > ...
                            > > It would be easier if size_t was
                            > > defined as a signed integer, and not a great deal would have been lost.
                            > > ...[/color]
                            >
                            > It wouldn't solve anything. It would just made the problem less obvious.
                            > In general case the difference between two signed integers does not fit
                            > into the range of the same signed integer type. The resultant signed
                            > type must be at least one bit longer than original types.
                            >[/color]

                            When that integer represents the size of something, it can only be positive
                            or zero. If it always possible to subtract two such positive signed numbers
                            without overflow. That's obvious isn't it?

                            john


                            Comment

                            • Andrey Tarasevich

                              #15
                              Re: negative number evaluating greater than string.size()

                              John Harrison wrote:[color=blue][color=green][color=darkred]
                              >> > ...
                              >> > It would be easier if size_t was
                              >> > defined as a signed integer, and not a great deal would have been lost.
                              >> > ...[/color]
                              >>
                              >> It wouldn't solve anything. It would just made the problem less obvious.
                              >> In general case the difference between two signed integers does not fit
                              >> into the range of the same signed integer type. The resultant signed
                              >> type must be at least one bit longer than original types.
                              >>[/color]
                              >
                              > When that integer represents the size of something, it can only be positive
                              > or zero. If it always possible to subtract two such positive signed numbers
                              > without overflow. That's obvious isn't it?
                              > ...[/color]

                              Yes, but the trade-off in this case is that you can only use half of the
                              type's range.

                              --
                              Best regards,
                              Andrey Tarasevich

                              Comment

                              Working...