简体   繁体   English

ISO/IEC 9899:2011 6.7.6.3 语义 7 是什么意思?

[英]What is ISO/IEC 9899:2011 6.7.6.3 Semantics 7 means?

first at all, here is the original(ISO/IEC 9899:2011 6.7.6.3 Semantics 7):首先,这里是原文(ISO/IEC 9899:2011 6.7.6.3 Semantics 7):

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.如果关键字static也出现在数组类型派生的 [ 和 ] 中,那么对于函数的每次调用,相应实参的值应提供对数组的第一个元素的访问,该元素至少与指定的元素一样多由大小表达式。

In order to better understand this problem, I write some code:为了更好的理解这个问题,我写了一些代码:

#include <stdio.h>

void f(int a[static 10][10])
{
    printf("0x%p\n", a);
}

int main(int argc, char *argv[])
{
    int a[][10] = {[20] = {1, 2}};
    f(a);
    int b[][10] = {{3, 4}};
    f(b);
    return 0;
}

what is that static (int a[static 10][10]) stand for? static (int a[static 10][10]) 代表什么?
Is the behavior of that code undefined?该代码的行为是否未定义? why?为什么?
Why do we only need to provide access to the first element of an array not all of it?为什么我们只需要提供对数组第一个元素的访问而不是所有元素?

what is that static(int a[static 10][10]) stand for? static(int a[static 10][10]) 代表什么?

As it is written in the quote provided by you:正如您提供的报价中所写:

... 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 . ...如果关键字静态也出现内的[和]的数组类型的推导,然后为每个呼叫的功能,相应的实际参数的值应提供访问与至少一样多的阵列的第一个元素由大小表达式指定的元素

Is the behavior of that code undefined?该代码的行为是否未定义? why?为什么?

Yes, the behavior of a function will be undefined if it expects an array with the specified number of elements in the size expression but the corresponding argument of the array type has fewer elements, because the function will try to access memory beyond the number of elements in the array used as an argument.是的,如果函数的行为在 size 表达式中需要指定数量的元素,但数组类型的相应参数具有较少的元素,则该函数的行为将是未定义的,因为该函数将尝试访问超出元素数量的内存在用作参数的数组中。

Why do we only need to provide access to the first element of an array not all of it?为什么我们只需要提供对数组第一个元素的访问而不是所有元素?

We need to provide access at least to the number of elements specified in the parameter declaration in the size expression because it is a contract between the function and the user of the function.我们需要至少提供对 size 表达式中参数声明中指定的元素数量的访问,因为它是函数和函数用户之间的契约。 The function expects at least the number of elements specified in the size expression.该函数至少需要大小表达式中指定的元素数。

The purpose of the qualifier is to let a compiler know that it may, at its leisure, generate code which reads any portion of the array without regard for whether or not it will be needed.限定符的目的是让编译器知道它可以在空闲时生成读取数组任何部分的代码,而不管是否需要它。 As an example of when that might be useful, consider something like:作为何时可能有用的示例,请考虑以下内容:

int get_first_positive_of_four(int arr[static 4])
{
  if (arr[0] > 0) return arr[0];
  if (arr[1] > 0) return arr[1];
  if (arr[2] > 0) return arr[2];
  if (arr[3] > 0) return arr[3];
  return 0;
}

Some processors have an instruction that will read multiple words of memory into registers in a manner that's faster than repeated single reads.一些处理器有一条指令,它将以比重复单次读取更快的方式将多个内存字读入寄存器。 On the ARM7-TDMI, for example (somewhat obsolete architecture, but its instruction timing is easy to reason about), an instruction that loads one word into a register takes 3 cycles, and one that loads N words takes 2+N cycles.例如在 ARM7-TDMI 上(架构有些陈旧,但其指令时序很容易推理),一条指令将一个字加载到寄存器中需要 3 个周期,而一个加载 N 个字的指令则需要 2+N 个周期。 Thus, loading all four words with one instruction will take the same amount of time as performing two separate loads.因此,用一条指令加载所有四个字将花费与执行两个单独加载相同的时间。 If a compiler were to unconditionally load all four words, this code would take longer than a version which uses individual loads in the case where only the first value gets used, the same amount of time if two values get used, and less time if three or four values get used.如果编译器要无条件地加载所有四个字,则此代码将比使用单独加载的版本花费更长的时间(在仅使用第一个值的情况下,如果使用两个值则时间相同,如果使用三个值则时间更少)或使用四个值。

In the event that the function was passed a two-item array which was located right at the end of accessible memory, and whose second value is positive, behavior would be defined in the absence of the static qualifier, but the version of the code which preloads all four values might trap with a memory fault.如果函数被传递一个位于可访问内存末尾的双项数组,并且其第二个值为正,则行为将在没有静态限定符的情况下定义,但代码的版本预加载所有四个值可能会因内存故障而陷入困境。 Thus, that optimization wouldn't be valid without the static qualifier, but would be valid with it.因此,如果没有静态限定符,该优化将无效,但对它有效。

I don't know to what extent compilers exploit this qualifier, but it allows compilers to generate code that speculatively reads data that may or may not end up being required, which may allow faster execution than would be possible if the speculative reads of such data were forbidden.我不知道编译器在多大程度上利用了这个限定符,但它允许编译器生成推测性读取数据的代码,这些数据最终可能需要也可能不需要,这可能允许比推测读取此类数据时更快的执行速度被禁止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM