Click to See Complete Forum and Search --> : C loop around case statements


beowulfe
February 16th, 2005, 03:22 PM
A while back, a buddy of mine happened to show me an interesting code segment that I didn't even know was valid C code. It was something like the following format:


switch(aByte)
{
while(someCondition)
{
case 0:
doSomething;
case 1:
doSomething;
case 2:
doSomething;
case 3:
doSomething;
case 4:
doSomething;
case 5:
doSomething;
case 6:
doSomething;
case 7:
doSomething;
}
}

According to a web site I found back then, the developer who came up with such a screwy code structure worked for some graphics company, and the purpose of the particular segment was a very efficient way to copy graphics data from one place to another. It also stated that there was a question among the ANSI committee whether it SHOULD be legal C (apperently, the compiler the writer used did compile this code), and was finally determined that it is "valid" code.

I, unfortunately, can't find anything about this in any of my web searches. I do remember that the code was called "<Something's> Machine" (I think named after the original author.......).

Does anyone have any idea what this was actually called, and where I can get information on it?

ejmw
February 16th, 2005, 03:42 PM
It looks like a possible way to implement a finite state machine or Turing machine in C (where aByte would be an indicator of the current state). I don't know about finding anything about the original code, but that might give you somewhere to start to look up the idea behind it.

Hobson
February 16th, 2005, 03:50 PM
Not many people would ever think that it is valid code:

cin >> number;

switch (number)
{
case 4:
cout << "Value OK!" << endl;
break;
default:
cout << "Wrong value!" << endl;
if (number>10)
{
cout << "Not only wrong, but also too much!" << endl;
break;
case 13:
cout << "Bad luck! You jumped inside of IF!" << endl;
}//if
}//switch

Everything becomes clear when you realize, that case ## is nothing else but label, which an be put anywhere inside of switch(#){...}.
Switch is a block, and it can contain other blocks, and labels can be placed inside of those internal blocks.
However, code you showed above is the first case I have ever seen use of this feature. For me it is as legible as using gotos.

Andreas Masur
February 16th, 2005, 03:54 PM
[ Moved thread ]

KevinHall
February 16th, 2005, 05:22 PM
I've actually seen this referenced in a few different books and websites on optimization. It's screwy, but legal. It does have a well-known name, but I am drawing a blank right now. If I find it, I'll post it.

Paul McKenzie
February 16th, 2005, 05:25 PM
I've actually seen this referenced in a few different books and websites on optimization. It's screwy, but legal. It does have a well-known name, but I am drawing a blank right now. If I find it, I'll post it.Duff's device maybe?

Regards,

Paul McKenzie

KevinHall
February 16th, 2005, 05:30 PM
It's called "Duff's Device" and here (http://www.lysator.liu.se/c/duffs-device.html) is a copy of a 1988 e-mail Tom Duff wrote on his trick. You'll also find lots of information about Duff's Device via Google.

KevinHall
February 16th, 2005, 05:34 PM
Paul, you beat me to it! :)

beowulfe
February 17th, 2005, 09:25 AM
YES! That's it, thanks much guys!