size_t help

This is a discussion on size_t help within the C++ Programming forums, part of the General Programming Boards category; I'm using the .find() function to search through a string like so Code: int i; i = string.find("whatever"); but i'm ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    10

    size_t help

    I'm using the .find() function to search through a string like so


    Code:
    int i;
    
    i = string.find("whatever");
    but i'm getting warnings that possible loss of data could occur due to a conversion from
    size_t to int.

    I was told that I could just ignore it, because it still works because the string isn't that big, but I'd like to see what I could do to fix this.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    Change the type to a size_t, what is so tricky?

    > because it still works because the string isn't that big
    Sooner or later, the casual attitude to types will bite you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    When i change it to size_t, the warnings go away, but I get a runtime error when I run the program.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    You might want to provide the smallest and simplest compilable code that demonstrates this runtime error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by Xanz1441 View Post
    When i change it to size_t, the warnings go away, but I get a runtime error when I run the program.
    Then there's something else wrong with the code, you know how to use the debugger?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Salem View Post
    Change the type to a size_t, what is so tricky?

    > because it still works because the string isn't that big
    Sooner or later, the casual attitude to types will bite you.
    On the other hand, it's kind of dumb for code to force the user to use specific types when all he really wanted is to figure out where something occurs in a string. You call one damn thing that uses size_t and now a bunch of size_t spreads through your code.

    This is one error I commit on purpose regularly, of course only after having thought about the consequences.

    Java completely did away with unsigned integer types for this among other reasons.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > of course only after having thought about the consequences.
    Face it, thinking ahead is not the usual fare around here.

    > On the other hand, it's kind of dumb for code to force the user to use specific types
    > when all he really wanted is to figure out where something occurs in a string.
    Either you have a type system or you don't.

    > You call one damn thing that uses size_t and now a bunch of size_t spreads through your code.
    Well if you ignored the problem to begin with, then sure the ripples will go a long way when you get around to fixing it.
    But if you use the correct type from the outset, it might just warn you when you're trying to add oranges to cabbage for example.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Salem View Post
    > On the other hand, it's kind of dumb for code to force the user to use specific types
    > when all he really wanted is to figure out where something occurs in a string.
    Either you have a type system or you don't.
    Well, yes. But the problem is not the type system, but the presence of so many signed and unsigned integer types of varying widths.

    But if you use the correct type from the outset, it might just warn you when you're trying to add oranges to cabbage for example.
    This is what I'm getting at:

    Code:
    int i;
    size_t n = strlen(something);
    
    for(i = 0; i < n; i++) /* Warns "Comparison between signed and unsigned" */
    Yes, "all" I have to do is make the loop iterator a size_t. But what if my loop had been like this:

    Code:
    for(i = -2; i < n; i++)
    Now there's just no way, unless I do this:

    Code:
    size_t i_plus_2;
    
    for(i_plus_2 = 0; i_plus_2 < n + 2; i_plus_2++)
    {
        /* Now all I have to do is subtract 2... Except when it would be negative. D'oh. */
    }
    Ultimately I end up doing this:

    Code:
    int size_t_to_int(size_t n)
    {
        if(n > INT_MAX)
        {
            blow_up();
        }
        return int(n);
    }
    It's all just silly, given that the length of the string is not going to be anywhere near the limit of an integer.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you know about the use of your variable, you can of course get rid of the warning by a cast to int (or whatever makes most sense in the circumstances).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by brewbuck View Post
    But what if my loop had been like this:
    Code:
    for(i = -2; i < n; i++)
    Why on earth would you have such a loop when iterating through something that has 0-based indices?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by CornedBee View Post
    Why on earth would you have such a loop when iterating through something that has 0-based indices?
    Iterating through an array might not be what is going on. Yeah, contrived example, but I have real ones too.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > But what if my loop had been like this:
    The number of times I've written a loop which iterates over a string say, starting at zero - lots and lots.
    The number of times I've needed to start at a negative index - zero (or something damn close to zero).

    Seems like a straw-man argument to me.

    > Now there's just no way, unless I do this:
    There's always another way:
    char *magicPtr = normalptr - 2;
    There, now you can index magicPtr using your mysterious -2 hack, but using a regular zero-based subscript.
    But the mere sight of such code would be flagging up all sorts of "what the hell..." warnings in my head.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Salem View Post
    There's always another way:
    char *magicPtr = normalptr - 2;
    But that creates a pointer value which is undefined, according to the standard.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,017
    It's just a warning. Use size_t unless you have a need for unsigned numbers. In that case, don't worry about comparing signed and unsigned types.

    For example:
    Code:
    size_t max = something.size();
    for(int i=max-1; i>=0; --i){
       //loop backwards
    }
    Here you can't use size_t for i, or you will get an infinite loop. But unless something has more than 3 million members, you won't run into trouble.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It should be possible to iterate in reverse like this:
    Code:
    for (std::size_t i = a.size(); i--; )
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21