CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2005
    Posts
    2

    C loop around case statements

    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?

  2. #2
    Join Date
    Dec 2004
    Location
    Ann Arbor, MI
    Posts
    281

    Re: C loop around case statements

    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.
    --EJMW

  3. #3
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: C loop around case statements

    Not many people would ever think that it is valid code:
    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.
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: C loop around case statements

    [ Moved thread ]

  5. #5
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: C loop around case statements

    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.
    Kevin Hall

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C loop around case statements

    Quote Originally Posted by KevinHall
    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

  7. #7
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: C loop around case statements

    It's called "Duff's Device" and here 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.
    Kevin Hall

  8. #8
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: C loop around case statements

    Paul, you beat me to it!
    Kevin Hall

  9. #9
    Join Date
    Feb 2005
    Posts
    2

    Re: C loop around case statements

    YES! That's it, thanks much guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured