• 0

[C#] Infinite Loop


Question

The following code produces an infinite loop and I was hoping someone could explain why. It has something to do with trying to use a byte value in a FOR loop. Since a byte is simply an 8-bit unsigned integer, I don't see why I shouldn't be able to use it natively in a FOR loop instead of typecasting a 32-bit integer when I write to the file.

using (FileStream fS = new FileStream(FileToWrite, FileMode.Create))
			using (BinaryWriter bW = new BinaryWriter(fS))
			{
				for (int i = 0; i < 10; i++)
				{
					for (byte j = 0; j <= 255; j++)  //  This for loop increments and writes correctly, but loops infinitely.
					{
						bW.Write(j);
					}
				}
				bW.Close();
			}

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Why not change it to int and be done with it? I see no reason why the loop counter has to be byte.

Link to comment
Share on other sites

  • 0
Why not change it to int and be done with it? I see no reason why the loop counter has to be byte.
I'm asking why it produces an infinite loop. I'm not interested in code opinions and am well aware that I could typecast an integer (as I stated in the original post).
Link to comment
Share on other sites

  • 0

Well, to my knowledge, incrementing a byte past it's upper bound reverts it back to zero since it cannot a value more than 255. So if you change your loop condition to "j < 255" you should exit the loop as you would expect.

edit: also note my workaround of changing the condition to "j < 255" will make it do 255 loops instead of the full 256 loops.

Edited by spacer
Link to comment
Share on other sites

  • 0

You can also set a conditional break point in the debugger and set a flag to break when j = 254, and step though the loop to see what j does.

Wesley

Link to comment
Share on other sites

  • 0

I see that C# offers the "checked" keyword to specify code that will throw an OverflowException. VB does this by default.

Link to comment
Share on other sites

  • 0
I see that C# offers the "checked" keyword to specify code that will throw an OverflowException. VB does this by default.

That's because checking for an overflow adds a lot of overhead cost and is not something that you want to do unless you absolutely have to. Using an int as a loop counter and casting it down to byte would be cheaper.

Also, overflows are quite necessary in everday tasks. For example, on an x86 CPU, if you take an 8-bit signed int -1 and add it to an 8-bit signed int 2, you will get: 0xFE + 0x02 = 0x01 (0xFF is -1, 0xFE is -2, 0x80 is -128). Overflows are how subtraction and negative numbers work. They're natural, more often than not, they are intended, and it's incumbent on the programmer to be cognizant of them because 99.9% of the time, an overflow is not an error.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.