在对C99的阵列大小的实际好处上,“保证”是指对C99的阵列大小进行“保证”。功能参数的功能? [英] On the practical advantage to C99's array size "guarantee" feature in function parameters?

查看:66
本文介绍了在对C99的阵列大小的实际好处上,“保证”是指对C99的阵列大小进行“保证”。功能参数的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99引入了新的函数参数表示法,其中 static 关键字可用于指定该参数至少包含N个元素。

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函数声明符,p7


将参数声明为类型数组应调整为
类型的合格指针,其中类型限定符(如果有)是在数组类型派生的[和]中指定的那些限定符。如果
关键字static也出现在数组类型
派生的[和]中,则对于函数的每次调用,对应的
实际参数的值应提供对第一个参数的访问。数组的
元素至少具有
大小表达式指定的数量。

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.

Eg

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

说x至少有10个元素。但这不是限制,因此不需要编译器发出诊断。

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.

C99原理对此状态:


[.. ]在某些系统上,
转换程序在函数开始时启动预取
或将通过参数引用的数组负载将是一个重大优势。
在C89中,用户无法向
转换器提供有关保证有多少元素可用的信息。

[..] 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.

在C99中,使用静态关键字:

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;
}



保证指针a和b均提供对第一个指针的访问包含至少十个元素的数组的
元素。静态
关键字还可以保证指针不是NULL,并且指向适当有效类型的
对象。

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.

理由似乎表明,要比C标准中的规定更有力。

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

基于这些事实:


  • 提供显着优势的任何实际系统都应包括在内。如基本原理所述?

  • 为什么C标准没有做出任何可能首先导致引入此功能的保证(如C99基本原理)?

(显然,更好的编译时诊断可能是一种用途-但这既不是显着的优势,也不是按预期的那样帮助优化。此外,编译器$ b如果$ b可以推断出潜在的空指针取消引用而没有这样的正式功能,则总是可以发出诊断信息。)

(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).

推荐答案

完全遵循您的问题,但我认为您可能对缺乏约束的概念感到困惑。这里需要。这不是一个约束,并且编译器没有义务发出诊断信息,因为编译器不一定能够在对其调用的点看到函数的定义。 静态数组大小保证是函数定义的属性,而不是函数 type / 声明。可能有多种原因,最有可能是不使用不兼容的函数类型进行声明。

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;
}

可以优化分支。

这篇关于在对C99的阵列大小的实际好处上,“保证”是指对C99的阵列大小进行“保证”。功能参数的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆