Unchecked Switch Statement

Chris Torek chris at umcp-cs.UUCP
Thu Mar 13 08:06:31 AEST 1986


In article <1706 at brl-smoke.ARPA> dietz%slb-doll.csnet at 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 at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.lang.c mailing list