c - Why is [static N] not enforced at compile-time? -
c99 has added static in function parameter (only meaningful in function definition, not declaration):
void func( int a[static 10] ) { if ( == null ) { /* branch can optimized out */ } printf("%d", a[-1]); /* causes ub */ }
however, meaning defined in c11 6.7.6.3/7 semantic, not constraint, means compiler should not issue diagnostic if function called incorrectly. in fact compiler must not abort compilation unless can prove ub caused in branches. example:
int main() { func(null); // ub int b[9]; func(b); // ub }
why did standard not make constraint (therefore requiring diagnostic)?
secondary question: why static
ignored in prototype (6.7.6.3/13), instead of being part of function signature? seems misleading allow prototype contain function body doesn't, , vice versa.
because violations cannot detected @ compile time in cases.
for example, argument pointer initial element of array allocated malloc()
. compiler cannot in general determine how big array is. nor, if argument pointer object, can compiler detect in general whether it's null.
the main purpose of feature not enforce restrictions on calls, enable optimizations. compiler may assume parameter points initial element of array of specified length. in cases, can enable better code generation.
but compilers can issue non-fatal warnings cases can detect. there no implication in standard such warnings should not issued.
Comments
Post a Comment