r/cpp • • Jun 23 '12

What is the upper limit on for loop iterations?

Can anyone explain why a for loop (seen below) runs indefinitely when the the end condition grows past a certain limit? (I'm running on widows vista, in eclipse with MinGw as my compiler)

//This code takes 8 seconds to run for (i = 0; i < 1000000000; i++) // 1x 109 { x++; }

//This code does not stop for (i = 0; i < 10000000000; i++) // 1x 1010 { x++; }

0 Upvotes

48 comments sorted by

3

u/cheese_man14 Jun 24 '12 edited Jun 24 '12

You're hitting the overflow before the iterator reaches that number. A signed int (32-bit architecture assumed) will have a max of 2,147,483,647 and a min of -2,147,483,648. Once you go over the max, you'll flip that sign bit and start counting from the negative value up. Since your loop checks to see if the value is still less than 10,000,000,000 it'll just keep going.

You can try assigning 10000000000 to a variable, but the value will be truncated at 32 bits, giving you 1,410,065,408. If you need a larger number, use a larger datatype like unsigned long long. This should give you 64-bits which has a huge max (1.8 x 1020, or something like that)

2

u/smccorm007 Jun 24 '12

Excellent. I thought overflow was the issue but was lacking the actual details of the limits. Thanks for your help.

2

u/Drainedsoul Jun 23 '12

What type is i?

3

u/smccorm007 Jun 23 '12

I have used int, float and unsigned long and all types yield the same result.

2

u/Drainedsoul Jun 23 '12

What size is unsigned long on your compiler?

I.e. if you do cout << sizeof(unsigned long) << "\n"; what do you get?

2

u/smccorm007 Jun 23 '12

So the size of my unsigned long is 4 bytes. Which has a maximum value of 4,294,967,295. So it looks like an overflow problem, I'm surprised that there is not an error or warning from my system. Thanks for your help it makes more sense now.

1

u/Drainedsoul Jun 23 '12

Try unsigned long long.

1

u/dakotahawkins Jun 23 '12

Does MinGW support that?

2

u/Drainedsoul Jun 23 '12

I don't know, been a while since I've used it.

-2

u/dakotahawkins Jun 23 '12

At least 4 (32 bits.) Look at the two links I posted.

edit: 10000000000 is bigger than 32 bits can hold.

0

u/Drainedsoul Jun 23 '12

Yes that's obvious.

I'm trying to make sure he isn't using a 64-bit compiler which has unsigned long as 8 bytes.

-1

u/dakotahawkins Jun 23 '12

if he was it would have worked

edit: I think I confused you with the OP when I told you to look at my links. I want OP to look.

1

u/Drainedsoul Jun 23 '12

Maybe he's not copy/pasting his code exactly.

I mean we already know we're not seeing all his code because i isn't declared in what he's shown us...

-1

u/dakotahawkins Jun 23 '12

Also, it should be at least a warning (not sure about MinGw, but I think CodePad uses GCC, where it's an error.) http://codepad.org/rIhXyCN4
edit: Thanks for the downvotes anyway.

-1

u/Drainedsoul Jun 23 '12

I don't think you have any idea what you're talking about.

It's perfectly legal to declare a variable outside the initialization step of a for loop and then use it in the for loop initialization step.

int i; for (i=0;i<n;++i) { }

So what I'm saying is that his code doesn't declare i therefore we know there's code he's not showing us therefore jumping to conclusions about what his problem is is kinda foolish.

Sure, it's likely that the issue is an integer overflow, and that's what I'm trying to check for, but you can't say with 100% certainty.

You've pasted enough shit from codepad. I know how C and C++ work. Please just leave.

2

u/dakotahawkins Jun 23 '12

Fun relevant fact. Back in the day, variables declared in the initialization step of a for loop had scope outside of the loop's block. One of the compilers we used (for solaris) carried that forward by default a long time after the standard changed, and caused lots of fun solaris-only problems until we found the compiler flag to enable the standard behavior.

→ More replies (0)

1

u/dakotahawkins Jun 23 '12

I know that, I'm only trying to demonstrate what we're both telling him, jesus.
Edit: And I'm not trying to demonstrate it to you because you clearly know what you're talking about. I'm trying to demonstrate it to the OP.

→ More replies (0)

3

u/aveceasar Jun 23 '12

Ask yourself these questions:

  1. What is the largest integral number that can be stored in 32 bits?

  2. For the floating point number - what happens here: 0.12345 * 106 + 1.0 if there's only 5 digit precision?

-1

u/dakotahawkins Jun 23 '12

3. Just don't use floats.

2

u/aveceasar Jun 23 '12

Really?

-2

u/dakotahawkins Jun 23 '12 edited Jun 23 '12

I mean use doubles unless you know exactly what you're doing and can guarantee you'll never need much precision or accidentally cause a loss of precision down the road.

Edit: relevant reply to ISvengali here.

2

u/aveceasar Jun 23 '12

That so?

double x = pow(2.0, 53);
double y = x + 1.0;
double z = y - x;

What is the value of z?

1

u/dakotahawkins Jun 23 '12

Yes, you can also overflow doubles.

3

u/aveceasar Jun 23 '12

Actually, there's no overflow in my example... :)

1

u/dakotahawkins Jun 23 '12

Haha. Also happens with floats though, but those are neat side effects.

3

u/ISvengali Jun 23 '12

Ide say the opposite. Use floats, and figure out how to properly handle them early on easy problems rather than hiding them for a tiny bit with doubles.

Usually if you have a precision problem, errors grow so fast the double doesnt give you that much padding anyway.

One of the most illuminating problems I worked on with floats was building a Quake (1!) Map -> BSP builder for my own engine. Map is the source format for quake maps (rather than just directly using the prebaked BSPs which are much easier to deal with). This required having a really firm handle on how floating point numbers behave when you have to do a lot of math on them and served me well from then on.

By all means, play with floats early and often and get a good grasp of them.

2

u/dakotahawkins Jun 23 '12

Fair enough. I guess my advice stems from working with other people who may or may not grasp (or care to grasp) the implications of floating point precision. I work with a lot of awesome programmers/computer scientists, but also with a lot of awesome non-software engineers (mechanical, structural, civil, etc.) who sometimes have to write code. Also, I'll admit to being a bit snarky about it.

2

u/schnarf Jun 26 '12

But definitely don't use a floating point type for an index variable in a for loop. You'll end up with loss of precision problems.

3

u/dakotahawkins Jun 23 '12 edited Jun 23 '12

Yeah, I'm going to go ahead and guess signed integer overflow.

edit: http://codepad.org/Q38auVkd
I had to use a short int because on whatever platform that is both the int and the long were 32 bits.

edit2: http://codepad.org/EsZdAnOP
Added unsigned variants.

2

u/zokier Jun 23 '12

This (or equivalent) document is what you should be looking at.

Also, try to compile your code with -Wall -Wextra and you'll get something like warning: comparison is always true due to limited range of data type. imho you should try always use at least those warning flags, and actually read the warnings too.

1

u/smccorm007 Jun 24 '12

Good reference. I'll take a look at adding the additional compiler arguments as well. I just started working with eclipse/CDT so I'm sure there are lots of configurations I can optimize.

0

u/[deleted] Jun 23 '12

[deleted]

1

u/zokier Jun 23 '12 edited Jun 23 '12

Afaik integer literals (without suffix) are the smallest type that can hold the value. In this case the literal would 64 bits. I'm not sure if there is a suffix to denote that you want (32-bit) int instead.

edit: http://ideone.com/l1xqx demonstrates the situation

12

u/JpDeathBlade Jun 23 '12

What you are doing is overflowing the variable. You said you used an integer to do this so I will go off that. Currently most int's are 32 bits in length (32 1's and 0's that store a number). If you were to turn them all on the maximum value you could store is 2,147,483,647. If you were to add on to this number, it would act the same way all other additions would (I will use a smaller number for example).

11 is 2 in binary. If you add one to this, the first one turns into a 0 and a 1 is carried over to the next column. Because these are both 1 it turns to 0 and another one gets carried over so you end up with 100. The same thing happens but on a larger scale and your number "overflows". The number is right but the value isn't large enough to hold the next number so it discards the larger values and stores the lower ones, which is all 0's. Floats hold less because of how you store floating point numbers, and longs would hold more because they are normally 64 bits (this would work provided your compiler is set up long to be 64 bits).

All in all though, if you have a loop that big you are most likely doing something wrong or inefficient (or working with amazingly large datasets, like geospacial stuff). Hope this helps, if you are still confused feel free to ask more questions.

7

u/[deleted] Jun 23 '12

11 is 3 in binary because 1 * 1 + 1 * 2 = 3.

Edit: formatting

3

u/dakotahawkins Jun 23 '12

11 is 2 in unary!

6

u/GrantSolar Jun 23 '12

11 is 11 in base 10. Not that that helps seeing as every base is base 10

1

u/zane17 Jun 24 '12

11 is 2 in reflected binary.

-2

u/JpDeathBlade Jun 23 '12

There is an easier way to figure out binary. Think of each column as a power of 2, like so:

64 32 16 8 4 2 1

Then you can put a 1 in each column until it adds up to the number you want. So 3 would be

64 32 16 8 4 2 1

0 0 0 0 0 1 1

or if you wanted to find 42

64 32 16 8 4 2 1

0 1 0 1 0 1 0

Unless you were trying to troll me...then 3 is actually 010 in binary because math. =)

2

u/ZDzb2v338PTyNzVrfXDW Jun 23 '12 edited Jun 23 '12
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = 10
1011 = 11
1100 = 12
1101 = 13
1110 = 14
1111 = 15

The max value you can store in 4 bits is 15. The max number of values (including zero) is 16. 24 = 16.

From right to left, the first column is 1, then 2, then 4, then 8, etc. So 010 would be 2 since there is only a 1 in the 2nd column (21). 011 would be (21) + (20), would be 2 + 1 = 3. Also odd numbers always have a 1 as the rightmost digit, so you know at first glance that 010 is an even number.

1

u/BitRex Jun 26 '12

GCC 4.6.1 warns about it. Are you sure you have all warnings on?