什么是“静态”在C组参数数组类型的含义? [英] What is the meaning of “static” in parameters array types in C?

查看:167
本文介绍了什么是“静态”在C组参数数组类型的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到下面稍微复杂的函数定义。

I saw following little complicated function definition.

void foo(double A[static 10]) {
   double B[10];    
}

是不是有效的C&放大器; C ++ code?难道新的语法由C99或C ++标准出台?什么是它的目的是什么?什么时候应该使用它?什么是这方面的需要?

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?

推荐答案

这C99符号,无效美孚(双A [静态10]),表示该功能可假设 A 点至10有效参数(从 * A A [9] )。记号使程序更多的信息,帮助编译器既优化功能,并检查它是否在每次调用点正确调用产生的code。

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标准,在将其引入的符号,这是通过子句6.7.5.2和6.7.5.3:7覆盖

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.

...如果关键字static也出现在了[和]数组类型推导,然后为每个调用该函数,相应的实际参数的值应提供访问数组的第一个元素至少许多元素的大小前pression指定

"… 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"

一个常见用途是 F(INT P [静态1]),这是的概念引用的或多或少相当于(如反对指针)其他语言。锵警告说,如果这样的函数声明,并通过在调用点空指针(因为符号明确意味着 P 不会是 NULL )。不过,我最后一次尝试,锵没提醒了 F(&安培A + 1),这是一种耻辱(但编译器没有发出所有。警告发光的部分的警告是好的话):


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):

#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 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.


语法看起来可能有点陌生,但C99标准化委员会显然决定重用pre-现有的静态关键字未引起歧义的地方从而避免引入新的关键字(那些可能破坏现有的程序)。


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

这篇关于什么是“静态”在C组参数数组类型的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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