I saw following little complicated function definition.
我看到了一些复杂的函数定义。
void foo(double A[static 10]) {
double B[10];
}
Is it valid C & C++ code? Is it new syntax introduced by C99 or C++ standard? What is the purpose of it? When should I use it? What is the need for this?
它是有效的C和c++代码吗?它是由C99或c++标准引入的新语法吗?它的目的是什么?我应该什么时候使用它?有什么必要这样做?
1 个解决方案
#1
4
This C99 notation, void foo(double A[static 10])
, means that the function can assume that A
points to 10 valid arguments (from *A
to A[9]
). The notation makes programs more informative, helping compilers to both optimize the code generated for the function foo
and to check that it is called correctly at each call site.
这个C99符号void foo(double A[static 10])意味着函数可以假设A指向10个有效参数(从*A到[9])。这种表示法使程序更加信息丰富,有助于编译器优化为函数foo生成的代码,并检查在每个调用站点上正确地调用它。
In the C99 standard, in which the notation was introduced, this is covered by clauses 6.7.5.2 and 6.7.5.3:7.
在引入表示法的C99标准中,第6.7.5.2和6.7.5.3条对此进行了说明。
“… 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”
“……如果关键字静态也出现在[和]数组类型的推导,然后为每个调用函数,相应的实际参数的值应当提供一个数组的第一个元素至少尽可能多的元素指定的大小表达"
One common use is f(int p[static 1])
, which is more or less equivalent to the notion of reference (as opposed to pointer) in other languages. Clang warns if such a function was declared and is passed a null pointer at the call site (because the notation explicitly means that p
will not be NULL
). However, the last time I tried, Clang did not warn for f(&a + 1)
, which is a shame (but a compiler doesn't have to emit all warnings. Emitting some warnings is good already):
一个常用的用法是f(int p[static 1]),它或多或少等同于其他语言中的引用(与指针相对)。Clang警告如果声明了这样一个函数,并在调用站点上传递了一个空指针(因为这个符号明确表示p将不为空)。然而,上次我尝试的时候,Clang没有警告f(&a + 1),这很可惜(但是编译器不必发出所有警告)。发出一些警告已经很好了):
#include <stdio.h>
void f(int p[static 1])
{
printf("%d\n", *p);
}
int main(){
int a = 0;
f(&a + 1);
f(0);
f(&a);
}
Clang warns for the second call, but unfortunately not for the first one:
Clang警告第二次呼叫,但不幸的是第一次呼叫没有:
$ clang t.c t.c:11:3: warning: null passed to a callee which requires a non-null argument [-Wnonnull] f(0); ^ ~ t.c:3:12: note: callee declares array parameter as static here void f(int p[static 1]) ^~~~~~~~~~~ 1 warning generated.
The syntax may seem a little strange, but the C99 standardization committee obviously decided to reuse the pre-existing static
keyword in a place that did not cause ambiguity so as to avoid the introduction of a new keyword (that could possibly break existing programs).
语法看起来有点奇怪,但是C99标准化委员会显然决定在一个不引起歧义的地方重用预先存在的static关键字,以避免引入新的关键字(这可能会破坏现有的程序)。
#1
4
This C99 notation, void foo(double A[static 10])
, means that the function can assume that A
points to 10 valid arguments (from *A
to A[9]
). The notation makes programs more informative, helping compilers to both optimize the code generated for the function foo
and to check that it is called correctly at each call site.
这个C99符号void foo(double A[static 10])意味着函数可以假设A指向10个有效参数(从*A到[9])。这种表示法使程序更加信息丰富,有助于编译器优化为函数foo生成的代码,并检查在每个调用站点上正确地调用它。
In the C99 standard, in which the notation was introduced, this is covered by clauses 6.7.5.2 and 6.7.5.3:7.
在引入表示法的C99标准中,第6.7.5.2和6.7.5.3条对此进行了说明。
“… 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”
“……如果关键字静态也出现在[和]数组类型的推导,然后为每个调用函数,相应的实际参数的值应当提供一个数组的第一个元素至少尽可能多的元素指定的大小表达"
One common use is f(int p[static 1])
, which is more or less equivalent to the notion of reference (as opposed to pointer) in other languages. Clang warns if such a function was declared and is passed a null pointer at the call site (because the notation explicitly means that p
will not be NULL
). However, the last time I tried, Clang did not warn for f(&a + 1)
, which is a shame (but a compiler doesn't have to emit all warnings. Emitting some warnings is good already):
一个常用的用法是f(int p[static 1]),它或多或少等同于其他语言中的引用(与指针相对)。Clang警告如果声明了这样一个函数,并在调用站点上传递了一个空指针(因为这个符号明确表示p将不为空)。然而,上次我尝试的时候,Clang没有警告f(&a + 1),这很可惜(但是编译器不必发出所有警告)。发出一些警告已经很好了):
#include <stdio.h>
void f(int p[static 1])
{
printf("%d\n", *p);
}
int main(){
int a = 0;
f(&a + 1);
f(0);
f(&a);
}
Clang warns for the second call, but unfortunately not for the first one:
Clang警告第二次呼叫,但不幸的是第一次呼叫没有:
$ clang t.c t.c:11:3: warning: null passed to a callee which requires a non-null argument [-Wnonnull] f(0); ^ ~ t.c:3:12: note: callee declares array parameter as static here void f(int p[static 1]) ^~~~~~~~~~~ 1 warning generated.
The syntax may seem a little strange, but the C99 standardization committee obviously decided to reuse the pre-existing static
keyword in a place that did not cause ambiguity so as to avoid the introduction of a new keyword (that could possibly break existing programs).
语法看起来有点奇怪,但是C99标准化委员会显然决定在一个不引起歧义的地方重用预先存在的static关键字,以避免引入新的关键字(这可能会破坏现有的程序)。