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

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

问题描述

我看到了下面一些复杂的函数定义.

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

C & 是否有效?C++代码?它是 C99 或 C++ 标准引入的新语法吗?它的目的是什么?我应该什么时候使用它?这有什么必要?

解决方案

这个 C99 符号,void foo(double A[static 10]),意味着函数可以假设 A 指向 10 个有效参数(从 *AA[9]).该符号使程序信息量更大,帮助编译器优化为函数 foo 生成的代码,并检查它是否在每个调用点被正确调用.

在引入符号的 C99 标准中,第 6.7.5.2 和 6.7.5.3:7 条涵盖了这一点.

<块引用>

……如果关键字static也出现在数组类型派生的[和]中,那么对于函数的每次调用,对应的实参的值应提供对数组第一个元素的访问,至少与大小表达式指定的元素一样多"

<小时>

一个常见的用法是f(int p[static 1]),它或多或少等价于reference(相对于指针)的概念.语言.如果声明了这样的函数并在调用点传递了一个空指针,Clang 会发出警告(因为该符号明确表示 p 不会是 NULL).但是,上次我尝试时,Clang 没有为 f(&a + 1) 发出警告,这是一种耻辱(但编译器不必发出所有警告.Emitting 一些警告已经很好):

#include void f(int p[静态 1]){printf("%d
", *p);}int main(){int a = 0;f(&a + 1);f(0);f(&a);}

Clang 会在第二次调用时发出警告,但不幸的是不会对第一个调用发出警告:

<前>$ clang t.ct.c:11:3: 警告:null 传递给需要非空参数的被调用方[-Wnonnull]f(0);^~t.c:3:12:注意:被调用方在此处将数组参数声明为静态void f(int p[静态 1])^~~~~~~~~~~已生成 1 个警告.

<小时>

语法可能看起来有点奇怪,但 C99 标准化委员会显然决定在不会引起歧义的地方重用预先存在的 static 关键字,以避免引入新的关键字(可能会破坏现有程序).

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?

解决方案

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.

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.

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

#include <stdio.h>

void f(int p[static 1])
{
  printf("%d
", *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.


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天全站免登陆