Ben Bacarisse said:
Keith Thompson said:
Ian Collins <
[email protected]> writes:
Would it be acceptable for a language change to change (all be it
subtly) the behaviour of existing code?
What behavior would change?
Currently, arr is a VLA. If the rules were changed to make ncol a
constant expression, as it is in C++, arr would be an ordinary array
object, not a VLA. As far as I can tell, this would merely make some
things legal and well-defined that are currently either constraint
violations or undefined behavior. I can't think of any cases were valid
code would either become invalid or change its (currently defined)
behavior. Can you?
I think I can, but only in the most obscure way. For example:
const int n = 10;
int vla[n];
int (*vlap)[n] = &vla;
int i = 0;
sizeof vlap[i++];
must make i == 1 if vla is a VLA and must leave it at zero otherwise.
For simplicity I've snipped the discussion that got us here so I feel I
should re-state that I don't think this matters much (to put it mildly).
Interesting case. I think it illustrates a problem with C99's definition
of sizeof. C99 6.5.3.4p2 says:
If the type of the operand is a variable length array type, the
operand is evaluated; otherwise, the operand is not evaluated
and the result is an integer constant.
I think this both causes things to be evaluated that needn't be, and
doesn't cause things to be evaluated that should be, though it probably
matters only in obscure cases.
In the simplest case of applying sizeof to a VLA:
int r = rand() % 10 + 1;
int vla[r];
sizeof vla;
I'm not sure what it even means here to evaluate ``vla''. Certainly it
has to evaluate *something*, but that something is probably an anonymous
variable. (In particular, it's not r, since r can be modified without
affecting the VLA.)