Explanation, please!

Doug Gwyn gwyn at smoke.ARPA
Fri Aug 26 02:16:29 AEST 1988


In article <638 at paris.ICS.UCI.EDU> schmidt at bonnie.ics.uci.edu (Douglas C. Schmidt) writes:
-   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);
-   }

-Now, much to my surprise, this is not only valid C++, it is also valid C!
-Could some one please explain to me why this is so?

Could you explain why it shouldn't be?  "case" labels are just a
special form of label.  You can stick a label on most statements.

-It seems like the case 7-1 labels are actually nested inside the
-do {} while loop, and thus not in the scope of the switch

?? "Thus"?  Why would the labels go out of scope?  They're
definitely within the body of the switch.

-(should a break statement exit both the switch and the loop, or just one?!?!)

A "break" applies to whatever the innermost container is.  A "break"
between two of the "*to++ = *from++;" statements would exit the
do-while loop.

-Finally, Stroustrup asks the rhetorical question ``why would anyone
-want to write something like this.''  Any guesses?!

It's about the fastest way to move arbitrarily-aligned data in
portable C with a guarantee as to what happens in the case that the
data overlaps.  memcpy() doesn't guarantee anything for overlaps.
memmove() does, but that's a recent X3J11 invention that probably
doesn't exist on your system yet.



More information about the Comp.lang.c mailing list