简体   繁体   English

为什么灵活数组成员的静态初始化工作?

[英]Why does static initialization of flexible array member work?

I have written the following basic code for a menu:我为菜单编写了以下基本代码:

typedef struct Menu {
    char* title;
    unsigned num_submenus;
    struct Menu *submenu[];
} Menu;

Menu sub1 = {"Submenu 1", 0, {NULL}};
Menu sub2 = {"Submenu 2", 0, {NULL}};
Menu Main = {"Main Menu", 2, {&sub1, &sub2}};   /* No Error?! */

int main()
{
    printf("%s\n", Main.title);
    printf("%s\n", Main.submenu[0]->title);
    printf("%s\n", Main.submenu[1]->title);
}

Browsing through a few related questions it seems like the only way to use a flexible array member is to dynamically allocate memory to it.浏览一些相关问题,似乎使用灵活数组成员的唯一方法是为其动态分配内存。 However my compiler is perfectly happy to compile and run the code without any errors or warnings.然而,我的编译器非常乐意编译和运行代码而没有任何错误或警告。 Is this verboten?这是禁言吗?

I am using MinGW gcc 4.6.1 and compiling under C99 rules.我正在使用 MinGW gcc 4.6.1 并在 C99 规则下编译。

Initialization of flexible array member in this way is not allowed as per C standard.根据 C 标准,不允许以这种方式初始化灵活的数组成员。

C11: 6.7.2.1 Structure and union specifiers (p20-21): C11:6.7.2.1 结构和联合说明符 (p20-21):

21 EXAMPLE 2 After the declaration: 21 例 2 声明后:

 struct s { int n; double d[]; };

the structure struct s has a flexible array member d .结构 struct s有一个灵活的数组成员d [...] [...]

22 Following the above declaration: 22 根据上述声明:

 struct s t1 = { 0 }; // valid struct s t2 = { 1, { 4.2 }}; // invalid t1.n = 4; // valid t1.d[0] = 4.2; // might be undefined behavior

The initialization of t2 is invalid (and violates a constraint) because struct s is treated as if it did not contain member d . t2的初始化无效(并违反约束),因为struct s被视为不包含成员d [...] [...]

But, GCC allows the static initialization of flexible array:但是,GCC 允许灵活数组的静态初始化:

GCC Manual: 6.17 Arrays of Length Zero : GCC 手册:6.17 长度为零的数组

Instead GCC allows static initialization of flexible array members .相反, GCC 允许灵活的数组成员的静态初始化 This is equivalent to defining a new structure containing the original structure followed by an array of sufficient size to contain the data.这相当于定义一个包含原始结构的新结构,后跟一个足够大的数组来包含数据。 Eg in the following, f1 is constructed as if it were declared like f2 .例如,在下面, f1的构造就像它被声明为f2一样。

 struct f1 { int x; int y[]; } f1 = { 1, { 2, 3, 4 } }; struct f2 { struct f1 f1; int data[3]; } f2 = { { 1 }, { 2, 3, 4 } };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 灵活数组成员的静态初始化 - Static initialization of a flexible array member 灵活阵列成员的非静态初始化? - Non-static initialization of a flexible array member? 弹性数组成员的非静态初始化 - non-static initialization of a flexible array member C 编程 - 灵活数组成员的非静态初始化 - C Programming - Non static initialization of a flexible array member 为什么对具有灵活数组成员的结构的初始化无效但对固定大小的数组成员有效? - Why is this initialization of a structure with a flexible array member invalid but valid with an fixed size array member? 灵活的数组成员是否会增加结构的大小? - Does a flexible array member increase sizeof a struct? 使用指向指针数组的指针的成员进行静态结构初始化 - Static structure initialization with a pointer to array of pointers member 为什么 sizeof 应用于具有灵活数组成员的结构不会产生分配的空间? - Why does sizeof applied to a structure with a flexible array member not produce the allocated space? 有谁知道为什么这个数组初始化语句不起作用? - Does anyone know why this array initialization statement won't work? 为什么我无法检索灵活的阵列成员大小? - Why can't I retrieve my flexible array member size?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM