gpt4 book ai didi

c - 为什么一致性实现的行为会有所不同 w.r.t.具有内部链接的不完整数组类型?

转载 作者:行者123 更新时间:2023-12-04 11:39:07 24 4
gpt4 key购买 nike

示例代码(t0.c):

static int arr[ ];

int main( void )
{
return arr[ 0 ];
}

static int arr[ ] = { 0 };
调用:
$ gcc t0.c -std=c11 -Wall -Wextra
<nothing>

$ clang t0.c -std=c11 -Wall -Wextra
<nothing>

$ cl t0.c /std:c11 /Za
t0.c(1): error C2133: 'arr': unknown size

$ gcc t0.c -std=c11 -Wall -Wextra -pedantic
t0.c:1:12: error: array size missing in ‘arr’

$ clang t0.c -std=c11 -Wall -Wextra -pedantic
<nothing>
C11,6.2.5 类型,22:

An array type of unknown size is an incomplete type. It is completed, for an identifier of that type, by specifying the size in a later declaration (with internal or external linkage).


C11,6.9.2 外部对象定义,3:

If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.


C11,J.2 未定义行为,1:

An identifier for an object with internal linkage and an incomplete type is declared with a tentative definition (6.9.2).


问题:为什么一致的实现表现出不同的行为?哪一个行为正确?

最佳答案

根据引用的段落,特别是 6.9.2p3 和非规范的 J.2p1,很明显代码违反了这些条款,因此可能不会出现在 C standard 的第 4p5 节中定义的严格符合程序中。 :

A strictly conforming program shall use only those featuresof the language and library specified in this InternationalStandard. It shall not produce output dependent on anyunspecified, undefined, or implementation-defined behavior, andshall not exceed any minimum implementation limit


然而,实现可以自由定义在严格符合程序中不允许的扩展。使用此类扩展的程序是第 4p6 节中定义的符合程序:

The two forms of conforming implementation are hosted andfreestanding. A conforming hosted implementation shall acceptany strictly conforming program. [ ... details of freestanding omitted ... ] A conformingimplementation may have extensions (including additional libraryfunctions), provided they do not alter the behavior of anystrictly conforming program


根据编译器输出,似乎 MSVC 不支持这样的扩展,但 gcc 和 clang 支持。
此外,当 -pedantic 出现时,gcc 会正确禁用此功能。标志通过,强制严格遵守。该 clang 不会生成带有 -pedantic 的诊断信息似乎是一个错误。

关于c - 为什么一致性实现的行为会有所不同 w.r.t.具有内部链接的不完整数组类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68635519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com