problems/risks due to programming language, stories requ

Raymond Chen raymond at purina.berkeley.edu
Fri Mar 16 06:59:09 AEST 1990


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;

[It's been some time since I last programmed in Pascal, so I may
 have misplaced a semicolon or two.]

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.



More information about the Comp.lang.c mailing list