E
Eric Sosman
Malcolm said:We could add valid ranges to C without complicating things. Simply declare
int i 0 to N;
(means that i can hold any integer between zero and N, assuming N is a
variable in scope). There are no other changes to the language. If the
restraint is violated, beahviour is undefined. Which means that a decent
compiler will give you a crash, a bad one will plough on regardless.
Often the strategy adopted will be to abandon any checks for the final
release, hence you have a powerful debugging tool with no impact on
performance.
Observe that this is not the behavior the O.P. was looking
for. His (imperfect) illustration showed a "clamping" behavior,
where an attempt to assign a too-small or too-large value had
the effect of assigning the minimum or the maximum.
The problem is that to honour the restrictions, the compiler needs to
check every assignment. This isn't particularly complicated, but will
approximately halve the speed of the program.
The speed estimate is surprising; how did you derive it?
Especially, how did you calculate it for a case like
int i 0 to N;
scanf ("%d", &i);
.... where the assignment and (presumably) the range check occur
inside a separately-compiled library function that doesn't know
about the range limitation? Unless you've invented a whole new
system of decorated pointer types, I don't see how you can get
this to work at all -- and adding decorations to pointers doesn't
strike me as "without complicating things."