mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Peterson <joe@skyrush.com>
To: Cong Wang <amwang@redhat.com>
Cc: Alan Cox <alan@linux.intel.com>,
	linux-kernel@vger.kernel.org, akpm@linux-foundation.org,
	Greg Kroah-Hartman <gregkh@suse.de>
Subject: Re: [Patch] tty: move a definition out of switch block
Date: Tue, 8 Dec 2009 08:02:43 -0700	[thread overview]
Message-ID: <fd3b2d10912080702l313c4e39p2ce98b93a3ae494@mail.gmail.com> (raw)
In-Reply-To: <4B1E2626.9010005@redhat.com>

On Tue, Dec 8, 2009 at 03:10, Cong Wang <amwang@redhat.com> wrote:
> Well, in C99 6.5.4, it has a very good example to explain this.
> See this example:
>
> switch (xxx) {
>        int a = 1; //<-- not initialized
>        int b; //<-- seems to be skipped, but not
>        func(&a); //<-- skipped;
> case 1:
>        //...
>        break;
> case 2:
>        return a; //<-- uninitialized value;
> }

I agree there are some strange semantics within switch statements
regarding what happens to lines before the first "case", but nothing
about declaring variables there is illegal, and the behavior is
well-defined.  Yes, statements and initializers get skipped, but the
variables are still declared.  Neither "int a" nor "int b" above are
skipped or even "seem skipped"; it is only that the initialization of
a is not performed.  Declarations (that to not initialize) are just
declarations; they do not get "executed".  Putting them at the start
of a switch block simple shows that they exist in that scope.

> So why not just:
>
> int a = 1, b;
> switch (xxx) {
> case 1:
>        // blah blah
> }
>
> ? A first galance will know everything, no need to guess if
> 'switch' skips it or not.

Main reason: variables is used only within scope of the switch
statement.  Sure, it could be moved outside, but I'm not convinced
this is vital or proper or even more clear.  It would be a bug to try
to initialize such a variable or to try to execute statements at the
start of the switch block, but this is not being done.

-Joe

  reply	other threads:[~2009-12-08 15:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-12-01  7:54 Amerigo Wang
2009-12-01 15:44 ` Alan Cox
2009-12-03  8:36   ` Cong Wang
2009-12-03 11:13     ` Alan Cox
2009-12-08 10:10       ` Cong Wang
2009-12-08 15:02         ` Joe Peterson [this message]
2009-12-03 15:18     ` Joe Peterson

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=fd3b2d10912080702l313c4e39p2ce98b93a3ae494@mail.gmail.com \
    --to=joe@skyrush.com \
    --cc=akpm@linux-foundation.org \
    --cc=alan@linux.intel.com \
    --cc=amwang@redhat.com \
    --cc=gregkh@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    /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

Powered by JetHome