Re: [Patch] tty: move a definition out of switch block

From: Joe Peterson
Date: Tue Dec 08 2009 - 10:02:53 EST


On Tue, Dec 8, 2009 at 03:10, Cong Wang <amwang@xxxxxxxxxx> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/