Hacker News new | ask | show | jobs
by Someone 2264 days ago
If that’s the most perverse you’ve seen, I guess you haven’t seen Duff’s device, which mixes it with a do…while loop (code copied from http://www.catb.org/~esr/jargon/html/D/Duffs-device.html):

   register n = (count + 7) / 8;      /* count > 0 assumed */

   switch (count % 8)
   {
   case 0:        do {  *to = *from++;
   case 7:              *to = *from++;
   case 6:              *to = *from++;
   case 5:              *to = *from++;
   case 4:              *to = *from++;
   case 3:              *to = *from++;
   case 2:              *to = *from++;
   case 1:              *to = *from++;
                      } while (--n > 0);
   }
2 comments

I have no idea if this still works in modern C, but here's something kind of similar I've seen used in C circa 1986:

  switch (foo)
  {
    case 1:
      ...code just for case 1...
      if (0)
      {
    case 2:
        ...code just for case 2...
      }
      ...code for both case 1 and case 2...
      break;
    case 3:
      ...

  }
used when two cases have a common tail, and you have sufficient space constraints that you do not want to duplicate the common tail code, and you have sufficient time constraints (and maybe space constraints on the stack) that you don't want to put the common tail code in a subroutine and call it from both cases.
Oh my god. I’ve wondered about a way to do this but... this is just devious.
This is goto with slightly more code ;-)
That's not perversity, that's suicide by art.