Home » C# ProgrammingRSS

Compiler bug C#

I teach C# in a beginning programming course and had a student submit a project containing the following code that compiled and ran:

        for (int i = 1; i <= LIMIT; i++)
        {
            Total = Baby;
            Adult = Adult + Baby;
            Baby = Adult - Total;
            Console.WriteLine("\n\n{0}\t{1}\t{2}\t{3}", Month, Adults, Babies, Totals);
            Console.WriteLine("\n\n{0}\t{1}\t{2}\t{3}", i, Baby, Total, Adult);
            Console.Write("_____________________________");
        } while (Adult < CAGES) ;

 

To say the least, I was surprised that the code compiled. Surely this must be a compiler bug.

 

5 Answers Found

 

Answer 1

This is not a compiler  bug, though I will agree that it would be nice if the compiler could provide a meaningful warning in this situation.

The problem is really only the last line, which is a completely separate statement in C#.  This:

for (int i = 1; i <= LIMIT; i++)
{
      Total = Baby;
      Adult = Adult + Baby;
      Baby = Adult - Total;
      Console.WriteLine("\n\n{0}\t{1}\t{2}\t{3}", Month, Adults, Babies, Totals);
      Console.WriteLine("\n\n{0}\t{1}\t{2}\t{3}", i, Baby, Total, Adult);
      Console.Write("_____________________________");
} while (Adult < CAGES) ;

Can be rewritten (identically) as:

// for loop first...for (int i = 1; i <= LIMIT; i++)
{
      Total = Baby;
      Adult = Adult + Baby;
      Baby = Adult - Total;
      Console.WriteLine("\n\n{0}\t{1}\t{2}\t{3}", Month, Adults, Babies, Totals);
      Console.WriteLine("\n\n{0}\t{1}\t{2}\t{3}", i, Baby, Total, Adult);
      Console.Write("_____________________________");
} 


// Now there's a while loopwhile (Adult < CAGES) ;

The issue here has nothing to do with the for loop - it's really just the while loop body.

 

That can be rewritten identically as:

// The semi colon effectively creates an empty blockwhile (Adult < CAGES) 
{
}

Now, this may seem like a useless statement - however, it is not necessarily incorrect.

 

If "Adult" or "CAGES" is a volatile variable, it's possible a separate thread could be modifying them.  Granted, a spin loop like this is a very bad idea (for many reasons), but technically, it could be creating a situation where there is a block until a condition is met.

 

Granted, this would be bad code, even if it were done on purpose, and there would be many better ways to clean it up.  I, personally, would like to see the compiler add a warning for empty loop bodies, but it is 100% correct code  as far as the C# language specifications, so it's not a bug  - more of a feature request.

 

 

Answer 2

It's not a compiler  bug. It's a for loop followed by a while loop. The ";" at the end is an empty statement.

for(...) { ... }

followed by

while(...) ;

Since the statement is empty and the condition has no side effect, if Adult < CAGES is true at the end of the for, you have an infinite while loop.

 

Answer 3

Since the statement is empty and the condition has no side effect, if Adult < CAGES is true at the end of the for, you have an infinite while loop.

Louis,

As I pointed out, technically, this may not be infinite.  However, one of the two variables would need to be marked volatile and be modified in a different thread...

 

 

Answer 4

Since the statement is empty and the condition has no side effect, if Adult < CAGES is true at the end of the for, you have an infinite while loop.

Louis,

As I pointed out, technically, this may not be infinite.  However, one of the two variables would need to be marked volatile and be modified in a different thread...

 


Reed Copsey, Jr. - http://reedcopsey.com

Additionally, one of those symbols could be a property accessor, that invokes code.

For example

class Foo
{
	publicint x = 0;

	public Foo()
	{
		while( BAR < 10 );
	}

	int BAR
	{
		get
		{
			return ++x;
		}
	}
}


 

Answer 5

As I pointed out, technically, this may not be infinite.  However, one of the two variables would need to be marked volatile and be modified in a different thread...

Absolutely right. I unrightfully thought it was local variables.
 
 
Search for  Compiler bug C#
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter