简体   繁体   English

编译器为 C 中 const 结构的未初始化成员分配什么值?

[英]What value does the compiler assign to an uninitialised member of a const struct in C?

Say I have a struct typedef that is:假设我有一个struct typedef ,即:

typedef struct {
    int32_t memberOne;
    int32_t memberTwo;
} myStruct_t;

I instantiate a const of that type as follows:我实例化该类型的const如下:

const myStruct_t myConst = {.memberTwo = 32};

What does C say the compiler should set memberOne to be? C 说编译器应该将memberOne设置为什么? I've tried it, of course, and I happen to get 0 on the compilers I've tried, what I'm after here is does the C standard require uninitialised members of a const struct to be initialised to something or other by the compiler, ie would the code above be considered portable?当然,我已经尝试过了,而且我碰巧在我尝试过的编译器上得到了 0,我在这里追求的是 C 标准是否需要const struct的未初始化成员初始化为某种东西或其他东西编译器,即上面的代码会被认为是可移植的吗? Clause 6.7.9 of C99 says: C99 的第 6.7.9 条说:

If an object that has static or thread storage duration is not initialized explicitly, then:如果具有静态或线程存储持续时间的对象未显式初始化,则:

— if it has pointer type, it is initialized to a null pointer; — 如果它具有指针类型,则将其初始化为空指针;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero; — 如果它具有算术类型,则将其初始化为(正或无符号)零;

— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits; — 如果是聚合,则每个成员都根据这些规则(递归地)初始化,并且任何填充都被初始化为零位;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits; — 如果是联合,则根据这些规则(递归)初始化第一个命名成员,并将任何填充初始化为零位;

...but what about const s? ...但是const s 呢? Are they considered to be of static storage type, just without the static keyword?它们是否被认为是static存储类型,只是没有static关键字?

does the C standard require uninitialised members of a const struct to be initialised to something C 标准是否要求将 const 结构的未初始化成员初始化为某些东西

Yes, they are guaranteed to be set to zero/null pointers as long as you initialize at least one member explicitly.是的,只要您明确初始化至少一个成员,就可以保证将它们设置为零/空指针。 const plays no part in it. const不参与其中。

You've already found the relevant part about how objects with static storage duration are initialized.您已经找到了有关如何初始化具有静态存储持续时间的对象的相关部分。 Just keep reading the same chapter:继续阅读同一章节:

C17 6.7.9 §19 C17 6.7.9 §19

The initialization shall occur in initializer list order, each initializer provided for a particular subobject overriding any previously listed initializer for the same subobject;初始化应按初始化程序列表顺序进行,为特定子对象提供的每个初始化程序都将覆盖同一子对象的任何先前列出的初始化程序; all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.所有未显式初始化的子对象都应隐式初始化,与具有静态存储持续时间的对象相同。

C17 6.7.9 §21 C17 6.7.9 §21

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.如果大括号括起来的列表中的初始值设定项少于聚合的元素或成员,或者用于初始化已知大小数组的字符串文字中的字符少于数组中的元素,则聚合的其余部分应隐式初始化与具有静态存储持续时间的对象相同。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM