简体   繁体   English

全局结构的成员在 C 中是否默认初始化为零?

[英]Are the members of a global structure initialized to zero by default in C?

C 中全局或静态结构的成员是否保证自动初始化为零,就像未初始化的全局或静态变量一样

From the C99 standard 6.7.8/10 "Initialization":来自 C99 标准 6.7.8/10“初始化”:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.如果没有显式初始化具有自动存储持续时间的对象,则其值是不确定的。 If an object that has static 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; — 如果是聚合,则根据这些规则(递归地)初始化每个成员;
— if it is a union, the first named member is initialized (recursively) according to these rules — 如果是联合,则根据这些规则(递归地)初始化第一个命名成员

Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).由于全局变量和静态结构具有静态存储持续时间,因此答案是肯定的——它们是零初始化的(结构中的指针将被设置为 NULL 指针值,通常为零位,但严格来说并不需要如此)。

The C++ 2003 standard has a similar requirement (3.6.2 "Initialization of non-local objects"): C++ 2003 标准有类似的要求(3.6.2“非本地对象的初始化”):

Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.具有静态存储持续时间 (3.7.1) 的对象应在任何其他初始化发生之前进行零初始化 (8.5)。

Sometime after that zero-initialization takes place, constructors are called (if the object has a constructor) under the somewhat more complicated rules that govern the timing and ordering of those calls.在零初始化发生后的某个时间,构造函数被调用(如果对象有构造函数),则根据更复杂的规则来控制这些调用的时间和顺序。

Local variables are not initialized.局部变量没有被初始化。

struct foobar {
    int x;
};

int main(void) {
    struct foobar qux;
    /* qux is uninitialized. It is a local variable */
    return 0;
}

static local variables are initialized to 0 (zero)静态局部变量被初始化为 0(零)

struct foobar {
    int x;
};

int main(void) {
    static struct foobar qux;
    /* qux is initialized (to 0). It is a static local variable */
    return 0;
}

Global variables are initialized to 0 (zero)全局变量初始化为 0(零)

struct foobar {
    int x;
};
struct foobar qux;
/* qux is initialized (to 0). It is a global variable */

int main(void) {
    return 0;
}

A struct is no different in this manner than a normal static C variable.在这种方式下, struct普通的静态 C 变量没有什么不同。 The memory reserved for that struct is completely initialized to 0 if it's a static.如果它是静态的,则为该struct保留的内存完全初始化为 0。

是的,所有全局数据都被清零,包括结构、类和联合的成员。

All data in global part of the program is set to zero.程序全局部分的所有数据都设置为零。

The BSS segment also known as Uninitialized data starts at the end of the data segment and contains all uninitialized global variables and static variables that are initialized to zero by default. BSS 段也称为未初始化数据,从数据段的末尾开始,包含所有未初始化的全局变量和默认初始化为零的静态变量。 For instance a variable declared static int i;例如,声明为 static int i 的变量; would be contained in the BSS segment.将包含在 BSS 段中。

Bss segment . Bss段

I don't know why is it so hard to try it yourself btw.我不知道为什么自己尝试一下这么难 btw。

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

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