Missing default in switch
cbfalconer@my-deja.com
cbfalconer@my-deja.com
Tue Oct 26 09:20:00 GMT 1999
In article < yyCP3.21$pp1.523@dfw-read.news.verio.net >,
"T.E.Dickey" <dickey@shell.clark.net> wrote:
> > And some generate better code if you DON'T supply a default: label.
> > In fact, that is far more common, as is obvious if you think about
> > the code that is likely to be generated.
>
> perhaps - the instance where I've used this (and seen small
> improvements) has usually been folding existing cases into 'default'
> - including of course cases which did nothing except break, as noted
> elsewhere).
This can't happen where the compiler generates a jump table and initial
out of range tests (usually the fastest code). However the optimization
is valid when the generated code is the equivalent of a long "if ...
else if ... else" statement. Some compilers select the mechanism based
on the size of the (tentative) case table. The "if ... else if ..." has
the advantage of easily handling silly coding such as:
switch (i) {
case 1: onething();
case 100: hundredthing();
case 1000: kilothing();
}
which would create a monster jump table. Here a default would add
nothing to the case table, but would add an "else" to the "if ..."
construct.
Moral: if these things matter to you, know thy compiler.
--
Chuck Falconer
(cbfalconer@my-deja.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
More information about the Gcc-help
mailing list