Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about size_t type in loop

Reply
Thread Tools

about size_t type in loop

 
 
wenjie
Guest
Posts: n/a
 
      01-29-2007
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){
...
}

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      01-29-2007
On Jan 29, 10:56 am, "wenjie" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-29-2007
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
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-29-2007
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?
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      01-31-2007
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Call function address stored in type of size_t? Adam Warner C Programming 26 12-28-2004 05:15 AM
How to use maximum size of size_t type ? javadesigner C Programming 11 01-22-2004 05:03 PM



Advertisments