[net.lang.c] Unchecked Switch Statement

dietz%slb-doll.csnet@CSNET-RELAY.ARPA (Paul Dietz) (03/12/86)

This issue has probably been discussed before, but here goes...

There seems to be an philosophical difference in C between array
accesses and switch statements.  In array accesses the compiler is free
to generate code that does undefined things if the index is out of
bounds.  In contrast, switch statements must fall through if there is
no "default:" label and no "case" matches the switch expression.
A more consistent definition would have the compiler generate
code that checks for an out-of-bounds switch expression only when
a default label is present.  The default label could be placed last in
the switch statement to get the same behavior as current default-less
switches.

chris@umcp-cs.UUCP (Chris Torek) (03/13/86)

In article <1706@brl-smoke.ARPA> dietz%slb-doll.csnet@CSNET-RELAY.ARPA
writes:

>There seems to be an philosophical difference in C between array
>accesses and switch statements. ...  A more consistent definition
>would have the compiler generate code that checks for an out-of-bounds
>switch expression only when a default label is present.

I think the way you stated this is unclear; I am going to guess that
by this you mean writing (e.g.)

	switch (exp) {
	case c1: ...;
	case c2: ...;
	}
	nextstmt;

would give undefined results if `exp' were not `c1' or `c2': a
compiler might give a run-time error, or might not; and execution
might fall through to `nextstmt', or stop, or even just go someplace
at random, as though switch were internally implemented by that
particular compiler as

	pc <- switchaddrs[exp]

Given the assumption that this is what you meant, I would agree
if C allowed arbitrary array arrangements (e.g., sparse arrays).
In this case, arrays would be arrays of data, and switches would
be arrays of code.

Imagine:

	var = array[
		index;
	default:
		printf("%d: out of bounds array index\n", index);
		exit(1);
	];

(Ok, enough wild and crazy ideas.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu