problems/risks due to programming language, stories requ
Dave Kirsch
a563 at mindlink.UUCP
Thu Mar 15 11:10:16 AEST 1990
> raymond writes:
>
> Msg-ID: <1990Mar15.205909.7882 at agate.berkeley.edu>
> Posted: 15 Mar 90 20:59:09 GMT
>
> Org. : U.C. Berkeley
> Person: Raymond Chen
>
> In article <1306 at mindlink.UUCP> a563 at mindlink.UUCP (Dave Kirsch) writes:
> >C flaws? Do this in Pascal:
> >
> >switch (i) {
> > case 2 : /* Add 2 to j */
> > j++;
> > case 1 : /* Add 1 to j */
> > j++;
> > case 0 : /* Print j out */
> > printf("%d\n", j);
> > break;
> > default :
> > printf("Illegal value in switch.\n");
> > abort();
> >}
>
> Sure
>
> case i of
> 2 : begin j := j + 1; goto 1; end;
> 1 : begin
> 1: j := j + 1; goto 0; end;
> 0 : begin
> 0: writeln(j); end;
> else begin writeln('Illegal value in case.'); Halt; end;
> end;
>
> Remember: C's switch statement is a thinly-disguised computed goto.
> If you're going to use goto's you may as well use them clearly
> and explicitly.
>
> I never use fallthrough%. If I need fallthrough, I make the
> fallthrough EXPLICIT via goto's. That way EVERYBODY knows what I'm
> doing: You, me, and lint.
>
> And of course it incurs absolutely NO performance penalty. (The
> compiler can't cache register values across "case" labels since
> they are just labels for the computed goto.) The only possible
> penalty is from a compiler which lacks a peephole optimizer. (You
> just have to elide the redundant jump statement.)
> --
> % Well, rarely. I'll use fallthrough when writing my entries into the
> International Obfuscated C Code Contest.
Yuck! Let's do a bit on readability, that Pascal code is gross! Aye, it
works, but doesn't do much for readability. I can scan through the C code in
one pass and see what it does, I have to look at the Pascal example you have
there a few times just to figure out what your doing. Fall through DOES have
it's benefits, eg if I have a 100 line code piece in a case, but the case
before wants to do 10 lines before it, I don't want to copy the whole 100 lines
over to the other case [though I could, as most compilers (TC and MSC do) will
optimize and one compile one copy]. Jumping between cases with goto's doesn't
agree with MY programming style. Only time I EVER use goto is when I want to
jump out of two or more inner loops, rather than using a flag variable.
I guess it's a choice of taste, although the C code is still more effecient
than the Pascal code, as the C code 'falls through', and the Pascal code
generates two uncessary jumps with the gotos.
The fall through is NOT a computed goto. The actual code is linear. The C
compiler I use makes a jump table for each of the cases, loads up the correct
spot to jump to, and then jumps there. Once it's jumped to the 'case 2:' it
will execute 'j++;' then fall through to the 'case 1:'. There is no jump
between them. When you put a break in, it generates a jump.
--
_____________________________________________________________________
Dave Kirsch UUCP: {uunet,ubc-cs}!van-bc!rsoft!mindlink!a563
Voice: (604) 327-4404 a563 at mindlink.UUCP
Vancouver, British Columbia
More information about the Comp.lang.c
mailing list