473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

about size_t type in loop

there are many examples write the loop like this:

vector<intcoll;
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){
...
}

Jan 29 '07 #1
4 7814
On Jan 29, 10:56 am, "wenjie" <zhengwen...@gmail.comwrote:
there are many examples write the loop like this:

vector<intcoll;
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

Jan 29 '07 #2
wenjie wrote:
there are many examples write the loop like this:

vector<intcoll;
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
Jan 29 '07 #3
wenjie wrote:
there are many examples write the loop like this:

vector<intcoll;
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?
Jan 29 '07 #4
In article <11**********************@j27g2000cwj.googlegroups .com>,
zh*********@gmail.com 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.
Jan 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: tings | last post by:
for (int i=0;i < strlen(pathcmd);i++){//this line cause a warning warning C4018: '<' : signed/unsigned mismatch strlen returns a number of type 'size_t'. size_t is an unsigned type and you are...
3
by: J Wang | last post by:
Dear, could you tell me about the usage of "##" in preprossor give me some simple examples. thanks. I just got the example from "dissection C" as follows:
13
by: agentxx04 | last post by:
Hi. Our assignment was to creat a program that can find the average, median & mode of a #of integers. Here's my program: #include<stdio.h> int main() { int item; int a, b, t, mode; int...
18
by: Steffen Fiksdal | last post by:
Can somebody please give me some rules of thumb about when I should be using size_t instead of for example int ? Is size_t *always* typedef'd as the largest unsigned integral type on all systems...
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
5
by: edware | last post by:
Hello, I have some questions about the size_t type. First, what do we know about size_t? From what I have read I believe that it is an unsigned integer, but not necessarily an int. Am I correct?...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
27
by: pkirk25 | last post by:
Assume an array of structs that is having rows added at random. By the time it reaches your function, you have no idea if it has a few hundred over over 10000 rows. When your function recieves...
5
by: nembo kid | last post by:
In the following function, s shouldn't be a pointer costant (array's name)? So why it is legal its increment? Thanks in advance. /* Code starts here */ void chartobyte (char *s) { while...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.