about size_t type in loop

W

wenjie

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){
...
}
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
K

Kai-Uwe Bux

wenjie said:
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 said:
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
 
R

Ron Natalie

wenjie said:
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?
 
J

Jerry Coffin

[ ... ]
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top