编译器为 C 中 const 结构的未初始化成员分配什么值?
What value does the compiler assign to an uninitialised member of a const struct in C?
假设我有一个 struct
typedef
即:
typedef struct {
int32_t memberOne;
int32_t memberTwo;
} myStruct_t;
我实例化了该类型的 const
如下:
const myStruct_t myConst = {.memberTwo = 32};
C 说编译器应该将 memberOne
设置为什么?我试过了,当然,我碰巧在我试过的编译器上得到 0,我在这里之后的是 C 标准 require 未初始化的成员 const
struct
由编译器初始化为某些东西或其他东西,即上面的代码是否被认为是可移植的? 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;
...但是 const
呢?它们是否被认为是 static
存储类型,只是没有 static
关键字?
does the C standard require uninitialised members of a const struct to be initialised to something
是的,只要您显式初始化至少一个成员,就可以保证将它们设置为 zero/null 指针。 const
没有参与其中。
您已经找到了有关如何初始化具有静态存储持续时间的对象的相关部分。继续阅读同一章:
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
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.
假设我有一个 struct
typedef
即:
typedef struct {
int32_t memberOne;
int32_t memberTwo;
} myStruct_t;
我实例化了该类型的 const
如下:
const myStruct_t myConst = {.memberTwo = 32};
C 说编译器应该将 memberOne
设置为什么?我试过了,当然,我碰巧在我试过的编译器上得到 0,我在这里之后的是 C 标准 require 未初始化的成员 const
struct
由编译器初始化为某些东西或其他东西,即上面的代码是否被认为是可移植的? 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;
...但是 const
呢?它们是否被认为是 static
存储类型,只是没有 static
关键字?
does the C standard require uninitialised members of a const struct to be initialised to something
是的,只要您显式初始化至少一个成员,就可以保证将它们设置为 zero/null 指针。 const
没有参与其中。
您已经找到了有关如何初始化具有静态存储持续时间的对象的相关部分。继续阅读同一章:
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
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.