为什么要在函数头中声明 C 数组参数的大小?
谁能告诉我为什么我应该在函数头中指定 C 数组参数的大小?例如:
void foo (int iz[6]) { iz[42] = 43; }
With:
int is[2] = {1,2,3};
我们得到一个有用的错误。也许它有助于评论/文档?
Can anyone enlighten me as to why I should bother to specify the size of a C array argument in a function header? For example:
void foo (int iz[6]) { iz[42] = 43; }
With:
int is[2] = {1,2,3};
we get a useful error. Perhaps it helps with commenting/documentation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
,你不应该。当您尝试将数组传递给函数时,真正传递的是指向数组开头的指针。由于函数接收的将是一个指针,因此最好将其编写为明确的:
然后,由于现在很清楚该函数没有得到大小的线索,因此将其添加为单独的参数:
IMO, you shouldn't. When you try to pass an array to a function, what's really passed is a pointer to the beginning of the array. Since what the function receives will be a pointer, it's better to write it to make that explicit:
Then, since it's now clear that the function has been given no clue of the size, add that as a separate parameter:
这样做的唯一有意义的原因是出于文档目的 - 告诉未来的用户函数期望接收至少包含这么多元素的数组。但即便如此,这也是一个惯例——您必须事先与其他用户达成一致。无论如何,语言(编译器)都会忽略该大小。您的函数声明相当于
void foo(int iz[])
和void foo(int *iz)
。使其对编译器有意义的唯一方法是将其声明为,
这向编译器承诺该数组将至少有 6 个元素,这意味着编译器将能够使用该假设来优化该代码。此外,如果您确实想采用上述约定,那么专门使用
static
声明数组参数大小更有意义,因为该语言显式定义了此构造的语义。我不清楚你所说的“我们得到一个有用的错误”是什么意思。该代码
不包含任何约束违规。它会产生未定义的行为,但不需要在编译期间产生诊断消息。换句话说,不,我们不会从中得到任何“有用的错误”。
The only meaningful reason to do that is for documentation purposes - to tell the future users that functions expect to receive an array of at least that many elements. But even that is a matter of convention - something that you have to agree upon with other users in advance. The language (the compiler) ignores that size anyway. Your function declaration is equivalent to
void foo(int iz[])
and tovoid foo(int *iz)
.The only way to make it somewhat meaningful for the compiler is to declare it as
which acts as a promise to the compiler that the array will have at least 6 elements, meaning that the compiler will be able to optimize that code using that assumption. Moreover, if you really want to adopt the convention mentioned above, it makes more sense to declare array parameter sizes with
static
specifically, since the language explicitly defines the semantics of this construct.What you mean by "we get a useful error" is not clear to me. The code
does not contain any constraint violations. It produces undefined behavior, but it is not required to produce a diagnostic message during compilation. In other words, no, we don't get any "useful error" from this.
这是一条评论。数组在函数参数中被降级为指针。然而,即使编译器不读取注释,注释仍然有用。
It's a comment. Arrays are demoted to pointers in function parameters. Comments can still be useful however, even if the compiler doesn't read them.
恕我直言,在现代编译器版本(如现代 gcc)中,以前的答案现在是错误的。
事实上,在这些编译器的现代版本中,编译器使用函数头中指定的 arraw 大小来发出非常有用的警告。
例子 :
在 gcc 12 上,如果您指定 :
并使用 : 调用该函数,
编译器将发出警告,表明您可以在 f 中执行使用 32 个字符的操作,但您仅指向 6 个字节的内存空间。
所以:在现代编译器中,它有助于精确你的意图,并让编译器对你的意图和你编码的内容进行额外的检查。
完整示例:
将通过:
对 gcc testi.c -o my_executable 进行简单调用
Imho, in modern compiler version (like modern gcc), the previous answers are now wrong.
Indeed, in modern version of those compilers, the size of the arraw specified in the header of a function is used by the compiler to through very useful warning.
Example :
On gcc 12, if you specify :
And call the function with :
The compiler will through a warning that say that you may does things in f that use 32 char, but that you point only to a 6 bytes memory space.
So : in modern compiler, it help to precise your intention and to let the compiler do additional checking about the concordance of your intention and what you have coded.
Full example :
Will through :
for a simple call to gcc testi.c -o my_executable
当您想要告诉客户端代码它必须传递已定义大小的数组时,这是一个有用的注释,即:
然而,文档在代码检查中不会替换:
It is a useful comment when you want to tell to client code that it must pass an array of defined size, i.e:
Yet, documentation doesn't replace in code checks: