On the practical advantage to C99's array size “guarantee” feature in function parameters?

匆匆过客 提交于 2020-08-04 14:32:31

问题


C99 introduced a new function argument notation where the static keyword can be used to specify that the argument has at least N elements.

6.7.6.3 Function declarators, p7

A declaration of a parameter as ''array of type'' shall be adjusted to ''qualified pointer to type'', where the type qualifiers (if any) are those specified within the [ and ] of the array type derivation. 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.

E.g.

void func(int x[static 10])
{
    /* something */
}

says that x has at least 10 elements. But this is not a constraint and as such a compiler is not required to issue a diagnostic.

The C99 rationale on this states:

[..] It would be a significant advantage on some systems for the translator to initiate, at the beginning of the function, prefetches or loads of the arrays that will be referenced through the parameters. There is no way in C89 for the user to provide information to the translator about how many elements are guaranteed to be available.

In C99, the use of the static keyword in:

void fadd(double a[static 10], const double b[static 10]) {
    int i;
    for (i = 0; i < 10; i++) {
        if (a[i] < 0.0)
            return;
        a[i] += b[i];
    }
    return;
}

guarantees that both the pointers a and b provide access to the first element of an array containing at least ten elements. The static keyword also guarantees that the pointer is not NULL and points to an object of the appropriate effective type.

The rationale appears to suggest stronger guarantees than what's stated in the C standard.

Based on these facts:

  • Are there any practical systems where this provides "significant advantages" as stated in the rationale?
  • Why does the C standard make no such guarantees (as in the C99 rationale) that might have motivated the introduction of this feature in the first place?

(Obviously, better compile time diagnostics could be one use - but that's neither a "significant advantage" nor does it help optimizations as intended. Besides, compilers can always issue diagnostics if they deduce potential null pointer dereferencing without a formal feature like this).


回答1:


I don't entirely follow your question, but I think you may be confused about what the lack of a "constraint" here entails. It's not a constraint, and the compiler is not obligated to issue a diagnostic, because the compiler is not necessarily able to see the definition of the function at the point(s) of call(s) to it. The static array-size guarantee is a property of the function definition, not the function type/declaration. There are various possible reasons for this, the most likely being a desire not to make declarations with/without it incompatible function types.

Unfortunately, this limits the feature to being used only for optimization, not correctness checking, except in cases where the compiler (or linker) can see the mismatch. The "advantage" of the feature is that the compiler can make optimizations that read indices that would not be read on the abstract machine. For example, in:

int foo(int a, int b[static 1])
{
    if (!a) return 0;
    else return a & *b;
}

the branch can be optimized out.



来源:https://stackoverflow.com/questions/62843710/on-the-practical-advantage-to-c99s-array-size-guarantee-feature-in-function-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!