Skip to content

UB56⚓︎

author: Xiangzhi Liu.

Definition⚓︎

An identifier for an object is declared with no linkage and the type of the object is incomplete after its declarator, or after its init-declarator if it has an initializer (6.7).

Description⚓︎

声明了一个不完全类型的对象。

Code⚓︎

UB56.c
extern struct S;

int main() {
    char a[];
    char b[] = {};
    struct S s;
    char c[] = {0}; // correct
    return 0;
}

Configurations⚓︎

gcc version 4.9.2

Target: x86_64-w64-mingw32

MSVC _MSC_VER = 1928

Target: x86_64

Behaviors⚓︎

编译报错: [Error] array size missing in 'a' [Error] storage size of 's' isn't known

编译报错: E0070 不允许使用不完整的类型 line 4 E0070 不允许使用不完整的类型 line 6 E0029 应输入表达式 line 5 E1345 空初始值设定项对于带有未指定绑定的数组无效 line 5

Advice⚓︎

遇到此类问题编译器通常都会报错,基本不太需要额外注意。需要注意的是 UB56.c 中第 5 行,gcc 会将其视为零长数组,而 MSVC 等编译器则不会。