about size_t type in loop

Discussion in 'C++' started by wenjie, Jan 29, 2007.

  1. wenjie

    wenjie Guest

    there are many examples write the loop like this:

    vector<int> coll;
    for(int i = 0; i < coll.size(); ++i){
    ...
    }

    the function size() return type is size_t (unsigned int) ;
    I think if the size of coll bigger than the max value of int, it will
    infinite loop, is that true?
    If that is true, Why not use like this:

    for(size_t i = 0; i < coll.size(); ++i){
    ...
    }
     
    wenjie, Jan 29, 2007
    #1
    1. Advertising

  2. On Jan 29, 10:56 am, "wenjie" <> wrote:
    > there are many examples write the loop like this:
    >
    > vector<int> coll;
    > for(int i = 0; i < coll.size(); ++i){
    > ...
    >
    > }
    >
    > the function size() return type is size_t (unsigned int) ;
    > I think if the size of coll bigger than the max value of int, it will
    > infinite loop, is that true?


    Seems reasonable, yes.

    > If that is true, Why not use like this:
    >
    > for(size_t i = 0; i < coll.size(); ++i){


    I do, and I try to use unsigned int when I'm not comparing against a
    value of type size_t but which can't be negative. If you turn up the
    warnings in the compiler you can find lots of warnings about comparing
    a signed and unsigned value.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 29, 2007
    #2
    1. Advertising

  3. wenjie

    Kai-Uwe Bux Guest

    wenjie wrote:

    > there are many examples write the loop like this:
    >
    > vector<int> coll;
    > for(int i = 0; i < coll.size(); ++i){
    > ...
    > }
    >
    > the function size() return type is size_t (unsigned int) ;


    That may be the case for the implementation you are using. The standard says
    that vector<int>::size() returns vector<int>::size_type, which is an
    unsigned type big enough to represent any non-negative value of
    vector<int>::difference_type.

    > I think if the size of coll bigger than the max value of int, it will
    > infinite loop, is that true?


    (a) The return value of size will be squeezed in an int. Assuming that it
    does not fit, you get undefined or implementation defined behavior.

    (b) Assuming that coll.size() is converted to a negative int, the signed
    int variable i will overflow, at which point your program will encounter
    undefined behavior behavior again.

    (c) Assuming that on your implementation, signed overflow just wraps around,
    the variable i will however, just go through INT_MIN and approach
    coll.size() from below. Then the loop terminates. However: depending on
    what you are doing in the body of the loop, you will very likely trigger
    some out-of-bounds UB before.

    > If that is true, Why not use like this:
    >
    > for(size_t i = 0; i < coll.size(); ++i){
    > ...
    > }


    Much better. I would also consider:

    for ( vector<int>::iterator iter = coll.begin();
    iter != coll.end(); ++iter ) {
    }

    or

    for ( vector<int>::size_type i = 0; i < coll.size(); ++i ) {
    }

    or

    std::for_each( coll.begin(), coll.end(), some_function_object );

    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jan 29, 2007
    #3
  4. wenjie

    Ron Natalie Guest

    wenjie wrote:
    > there are many examples write the loop like this:
    >
    > vector<int> coll;
    > for(int i = 0; i < coll.size(); ++i){
    > ...
    > }
    >
    > the function size() return type is size_t (unsigned int) ;
    > I think if the size of coll bigger than the max value of int, it will
    > infinite loop, is that true?


    Actually it's undefined behavior when you increment a SIGNED integer
    beyond its maximum value.

    > If that is true, Why not use like this:
    >
    > for(size_t i = 0; i < coll.size(); ++i){
    > ...
    > }
    >

    Why not?
     
    Ron Natalie, Jan 29, 2007
    #4
  5. wenjie

    Jerry Coffin Guest

    In article <>,
    says...

    [ ... ]

    > If that is true, Why not use like this:
    >
    > for(size_t i = 0; i < coll.size(); ++i){
    > ...
    > }


    Because you should really be using an algorithm instead of a loop?

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Jan 31, 2007
    #5
    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. javadesigner

    How to use maximum size of size_t type ?

    javadesigner, Jan 21, 2004, in forum: C Programming
    Replies:
    11
    Views:
    1,283
    Dan Pop
    Jan 22, 2004
  2. Adam Warner

    Call function address stored in type of size_t?

    Adam Warner, Dec 22, 2004, in forum: C Programming
    Replies:
    26
    Views:
    897
    Chris Torek
    Dec 28, 2004
  3. Alex Vinokur
    Replies:
    9
    Views:
    817
    James Kanze
    Oct 13, 2008
  4. Alex Vinokur
    Replies:
    1
    Views:
    595
  5. Isaac Won
    Replies:
    9
    Views:
    407
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page