Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9120861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:43:21+00:00 2026-06-17T05:43:21+00:00

Only last night I encountered the curious Duff’s device for the first time. I’ve

  • 0

Only last night I encountered the curious Duff’s device for the first time. I’ve been doing some reading on it, and I don’t think it’s that daunting to understand. What I am curious about is the strange syntax (from Wikipedia):

register short *to, *from;
register int count;
{
  register int n = (count + 7) / 8;
  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);
  }
}

I was reading the C++ standard definition of a switch statement (please let me know if that’s outdated, I’m not familiar with Open-Std.org). So far as I can understand case statements are just simplified jump statements for use by the switch statement.

The switch itself completely ignores the nested do-while, and the loop ignores the case statements. Since the switch jumps inside of the loop, the loop is executed. The switch is there to cover the remainder (from the division by 8), and the loop handles the portion that is evenly divisible. This all makes sense.

My question then is why the awkward syntax? It occurs to me that the loop could be written such that all of the case statements are contained within, yes? I see nothing in the standard that prohibits this behavior, and it compiles correctly under GCC 4.7, so is the following considered legal?

register short *to, *from;
register int count;
{
  register int n = (count + 7) / 8;
  switch (count <= 0 ? 8 : count % 8)
  {
    do
    {
      case 0:         *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++;
      default:        ; // invalid count, suppress warning, etc.
    } while(--n > 0);
  }
}

To me this makes the intent of the code much clearer. Thanks for any feedback. 😉

Edit: As noted below, the original code was written for C and had implicit int for the count and n variables. Since I tagged it C++ I’ve modified that.

Edit 2: Modified the revised example code to account for invalid count values.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:43:23+00:00Added an answer on June 17, 2026 at 5:43 am

    Looking at the C++11 Standard, the part of the code I think you’re asking about would be allowed. Have you tried it?

    The rule I see that’s most applicable is:

    Note: Usually, the substatement that is the subject of a switch is compound and case and default labels appear on the top-level statements contained within the (compound) substatement, but this is not required.

    In fact, this means you could get rid of the braces around the do–while, and write

      int n = (count + 7) / 8;
      switch (count % 8) do
      {
          case 0:         *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);
    

    However, this line is NOT valid C++:

    register n = (count + 7) / 8;
    

    C++ does not allow default-int, the type of a variable must be specified or inferred.


    Oh here, fix the number of iterations without breaking formatting:

      int n = 1 + count / 8;
      switch (count % 8) 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++;
          case 0:                      ;
      } while(--n > 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading some of the SCJP certification last night, I got thinking about switch
I need to fetch only first record (because I need last date) of resultset,
Is that real to pull only one (first or last, no matter) photo from
Similar question from last night, I don't have access to edit the source HTML
This is weird. Only on mobile safari, and since last night this script.... function
My app had been chugging along fine on the Android market until last night
I was pondering this question last night while debugging some things in IE7, when
I created this image resizing plugin last night.The problem is if there is only
I'm new to jquery and just started reading up on json early last night.
I only noticed this last night, as I have not used the toolbox on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.