public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Bill Davidsen <davidsen@tmr.com>
To: Phani Kandula <phani.lkml@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: doubt about "switch" - default case in af_inet.c
Date: Thu, 16 Dec 2004 13:43:19 -0500	[thread overview]
Message-ID: <41C1D747.1040706@tmr.com> (raw)
In-Reply-To: <7d34f21904120905573ddb6d25@mail.gmail.com>

Phani Kandula wrote:
> Hi all,
> 
> I'm a newbie to the Linux. I'm using 2.6.8 kernel. 
> In /usr/src/linux/net/ipv4/af_inet.c I came across this...
>  <code>
>      switch (sock->state) {
>      default:
>      //do something..
>          goto out;
>      case SS_CONNECTED:
>      //do something..
>          goto out;
>      case SS_CONNECTING:
>      //do something..
>          break;
>      case SS_UNCONNECTED:
>      //do something..
>          break;
>      }
>  </code>
> 
> Is there any advantage in having 'default' as the first case? 
> My understanding is that it will be useful only when 'default' is the
> most likely case (in general).
> 
> Even then, my doubt: How will compiler (say gcc) implement 'default'
> as the first value? Program is supposed to see all the cases and then
> decide 'default'. Is this correct?
> 
> So, is this the best way to do it? please clarify..

Just so. The compiler will check some or all of the cases first, and 
then take the default case. If you look at the code with and without 
optimization (use -S) you will see that it behaves the same way 
regardless of the placement of the default case. There is one exception, 
that is the one where control falls through from one case to another, 
and the default case is not last and lacks a break, such that the 
default winds up executing the code from the case(s) following.

Do I have to say that the code doing stuff like that is hard to read?

-- 
    -bill davidsen (davidsen@tmr.com)
"The secret to procrastination is to put things off until the
  last possible moment - but no longer"  -me

      parent reply	other threads:[~2004-12-16 18:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-09 13:57 doubt about "switch" - default case in af_inet.c Phani Kandula
2004-12-09 14:09 ` linux-os
2004-12-16 18:43 ` Bill Davidsen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=41C1D747.1040706@tmr.com \
    --to=davidsen@tmr.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=phani.lkml@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox