4 ms·
The purpose of int foo(int x[static 10]) is not to produce a warning - that's just a nice possible side-effect (and only in some cases). The real purpose i
by caf 14y ago
The purpose of
int foo(int x[static 10])
is not to produce a warning - that's just a nice possible side-effect (and only in some cases).
The real purpose is to allow the compiler to optimise the compilation of the foo() function itself, under the assumption that x will always point to the first element of an array of at least 10 elements.
- tsahyt 14y agoAt least or exactly 10 elements?
- popee 14y agoAt least
- tsahyt 14y agoHuh, I didn't know that. I just checked the standard and you appear to be correct, so, just for the record: "... if the keyword static also appears within the [ and ] of the array type derivation, then for each call to the function, the value of the corresponding actual argument shall provide access to the first element of an array with at least as many elements as specified by the size expression."
- revelation 14y agoWhat possible optimization would a compiler be able to do, given that arrays are only ever implicit in compiled C?
- jedbrown 14y agoIf it knows the address is valid, it can use a speculative load. If it knows there are enough entries, it can use a wide load. Without that knowledge, such instructions could trigger SEGV due to an invalid pointer or the wide load spilling over a page boundary.