| Forum Home | ||||
| Press F1 | ||||
| Thread ID: 123371 | 2012-02-21 01:00:00 | I think I'm missing something with this C++ code... | Agent_24 (57) | Press F1 |
| Post ID | Timestamp | Content | User | ||
| 1260398 | 2012-02-21 01:00:00 | #include <iostream.h> void main() { int counter; for (counter = 20; counter <= 20; counter--) { cout << Going loopy << counter << endl; } } Why does this code work? The code is described as: This code loops forever because the termination condition can never be false because decrementing the value of counter takes it further away from being greater than 20, not closer. This would all make sense except that "<" is not greater then Clearly I am missing something here. Can anyone tell me what it is? Am I in fact, Chef Brian? |
Agent_24 (57) | ||
| 1260399 | 2012-02-21 01:41:00 | Try reading the explanation again - it is, in fact, (mostly) correct. Here's the problem in list format - does this help you understand it? The counter starts at 20. The loop will continue as long as the counter is less than or equal to 20. This is the same as continuing until the counter is greater than 20. The counter is decremented by one at the end of each loop iteration. Because the counter is decremented each iteration, the loop will continue until the integer overflows. The explanation fails to note that the loop will quit once the integer overflows and ends up positive again, however as this seems to be a teaching example I'm assuming that aspect has been deliberately ignored. If you're curious about what happens in this case, consider the following test program (C): #include <stdio.h> int main() { int i; for(i = 20; i <= 20; i--); printf("Overflow: %d\n", i); } Edit: The same thing in C++: #include <iostream.h> int main() { int i; for (i = 20; i <= 20; i--); cout << "Overflow: " << i << endl; } |
Erayd (23) | ||
| 1260400 | 2012-02-21 02:47:00 | Ahh, your explanation led me to notice what I had missed: That the loop runs while the termination condition results as true, not until it is true - (as I believed it did, for some weird reason) - now it makes sense! Didn't know the integer would end up positive upon overflowing either, thanks! |
Agent_24 (57) | ||
| 1260401 | 2012-02-21 03:05:00 | not until it is true If that were correct, the loop wouldn't execute at all as 20 <= 20 :) |
pcuser42 (130) | ||
| 1260402 | 2012-02-21 03:07:00 | You are quite right! Had my brain been working (which it clearly wasn't) I would probably have noticed that as well... | Agent_24 (57) | ||
| 1260403 | 2012-02-21 07:17:00 | Ahh, your explanation led me to notice what I had missed: That the loop runs while the termination condition results as true, not until it is true - (as I believed it did, for some weird reason) - now it makes sense! Didn't know the integer would end up positive upon overflowing either, thanks! Catches quite a few the old overflow error. Some old programs can't handle large HD's and large amounts of memory because the programmer did not use a large enough integer type. Had a case a while back where the program was saying there wasn't enough memory but taking a 512mb stick out made it happy. |
mikebartnz (21) | ||
| 1260404 | 2012-02-22 01:18:00 | The program given in the OP may appear to loop forever, as it will be displaying a line for each value of counter i.e.: Going loopy 20 Going loopy 19 Going loopy 18 ... Going loopy 1 Going loopy 0 Going loopy -1 Going loopy -2 and so on. Looping continues while counter is less than OR EQUAL to 20; If the program was left to continue the last line displayed would be: Going loopy -2147483648 I.e. over 2 billion lines will have been displayed. What is special about -2,147,483,648? In C++ an int is stored as a 32 bit binary number and permits a range in values from -2,147,483,648 to +2,147,483,647. In hexadecimal this is represented by 0x80000000. Decrementing this value by 1 gives 0x7FFFFFFF. Not only has 1 been subtracted from the value, overflow has caused the sign bit to be changed from 1 (negative) to 0 (positive). I.e. the int value has changed from -2,147,483,648 to +2,147,483,647. As the result is not less than or equal to 20, then the loop terminates. |
pogo (16708) | ||
| 1260405 | 2012-02-22 01:38:00 | What is special about -2,147,483,648? In C++ an “int” is stored as a 32 bit binary number and permits a range in values from -2,147,483,648 to +2,147,483,647.Actually, sizeof(int) is implementation-defined. 4 bytes (32bits) is a common value on a 32bit system, but there's nothing mandating this, and depending on the system architecture and the compiler in question it can easily be something else. The C/C++ standard requires that an int be at least 16 bits (2 bytes), but as long as it meets that requirement it could be anything. |
Erayd (23) | ||
| 1 | |||||