PDA

View Full Version : I think I'm missing something with this C++ code...



Agent_24
21-02-2012, 02:00 PM
#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?

Erayd
21-02-2012, 02:41 PM
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;
}

Agent_24
21-02-2012, 03:47 PM
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!

pcuser42
21-02-2012, 04:05 PM
not until it is true

If that were correct, the loop wouldn't execute at all as 20 <= 20 :)

Agent_24
21-02-2012, 04:07 PM
You are quite right! Had my brain been working (which it clearly wasn't) I would probably have noticed that as well...

mikebartnz
21-02-2012, 08:17 PM
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.

pogo
22-02-2012, 02:18 PM
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.

Erayd
22-02-2012, 02:38 PM
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.