C中静态变量的初始化

时间:2022-09-06 18:46:37

I have a question about the initialization of static variables in C. I know if we declare a global static variable that by default the value is 0. For example:

我有一个关于C中静态变量初始化的问题。我知道如果我们声明一个全局静态变量,默认情况下该值为0.例如:

static int a; //although we do not initialize it, the value of a is 0

but what about the following data structure:

但是以下数据结构如何:

typedef struct
{
    int a;
    int b;
    int c;
} Hello;

static Hello hello[3];

are all of the members in each struct of hello[0], hello[1], hello[2] initialized as 0?

hello [0],hello [1],hello [2]的每个结构中的所有成员都被初始化为0?

4 个解决方案

#1


42  

Yes, all members are initialized for objects with static storage. See 6.7.8/10 in the C99 Standard (PDF document)

是的,所有成员都被初始化为具有静态存储的对象。见C99标准中的6.7.8 / 10(PDF文件)

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.

如果未显式初始化具有自动存储持续时间的对象,则其值不确定。如果没有显式初始化具有静态存储持续时间的对象,则: - 如果它具有指针类型,则将其初始化为空指针; - 如果它有算术类型,则初始化为(正或无符号)零; - 如果是聚合,则根据这些规则初始化(递归)每个成员; - 如果它是一个联合,则根据这些规则初始化(递归)第一个命名成员。

To initialize everything in an object, whether it's static or not, to 0, I like to use the universal zero initializer

要将对象中的所有内容(无论是否为静态)初始化为0,我都希望使用通用零初始化程序

sometype identifier0 = {0};
someothertype identifier1[SOMESIZE] = {0};
anytype identifier2[SIZE1][SIZE2][SIZE3] = {0};

#2


2  

Yes, file-scope static variables are initialized to zero, including all members of structures, arrays, etc.

是的,文件范围静态变量初始化为零,包括结构,数组等的所有成员。

See this question for reference (I'll vote to close this as a duplicate, too).

请参阅此问题以供参考(我也会投票将其作为副本关闭)。


Edit: this question is getting much better answers, so I'm voting to close that question as a duplicate of this, instead.

编辑:这个问题得到了更好的答案,所以我投票决定将这个问题作为副本来关闭。

For reference, here is the C FAQ link from that question's accepted answer, although of course the C99 and C11 standards linked here are canonical.

作为参考,这里是该问题的接受答案的C FAQ链接,当然这里链接的C99和C11标准是规范的。

#3


1  

Yes, they are, as long they have static or thread storage duration.

是的,他们是,只要他们有静态或线程存储持续时间。

C11 (n1570), § 6.7.9 Initialization #10

C11(n1570),§6.7.9初始化#10

If an object that has static or thread storage duration is not initialized explicitly, then:

如果未显式初始化具有静态或线程存储持续时间的对象,则:

[...]

[...]

  • 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;
  • 如果是聚合,则根据这些规则初始化(递归)每个成员,并将任何填充初始化为零比特;

[...]

[...]

#4


1  

I would add that static variables (or arrays) are classified into two types.

我想补充一点,静态变量(或数组)分为两种类型。

Initialized are the ones that are given value from code at compile time. These are usually stored in DS though this is compiler specific.

初始化是在编译时从代码中获得值的那些。这些通常存储在DS中,尽管这是特定于编译器的。

The other type is uninitialized statics which are initialized at run time and are stored into BSS segment though again this is compiler specific.

另一种类型是未初始化的静态,它们在运行时初始化并存储到BSS段中,但这又是编译器特定的。

BSS

BSS

#1


42  

Yes, all members are initialized for objects with static storage. See 6.7.8/10 in the C99 Standard (PDF document)

是的,所有成员都被初始化为具有静态存储的对象。见C99标准中的6.7.8 / 10(PDF文件)

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.

如果未显式初始化具有自动存储持续时间的对象,则其值不确定。如果没有显式初始化具有静态存储持续时间的对象,则: - 如果它具有指针类型,则将其初始化为空指针; - 如果它有算术类型,则初始化为(正或无符号)零; - 如果是聚合,则根据这些规则初始化(递归)每个成员; - 如果它是一个联合,则根据这些规则初始化(递归)第一个命名成员。

To initialize everything in an object, whether it's static or not, to 0, I like to use the universal zero initializer

要将对象中的所有内容(无论是否为静态)初始化为0,我都希望使用通用零初始化程序

sometype identifier0 = {0};
someothertype identifier1[SOMESIZE] = {0};
anytype identifier2[SIZE1][SIZE2][SIZE3] = {0};

#2


2  

Yes, file-scope static variables are initialized to zero, including all members of structures, arrays, etc.

是的,文件范围静态变量初始化为零,包括结构,数组等的所有成员。

See this question for reference (I'll vote to close this as a duplicate, too).

请参阅此问题以供参考(我也会投票将其作为副本关闭)。


Edit: this question is getting much better answers, so I'm voting to close that question as a duplicate of this, instead.

编辑:这个问题得到了更好的答案,所以我投票决定将这个问题作为副本来关闭。

For reference, here is the C FAQ link from that question's accepted answer, although of course the C99 and C11 standards linked here are canonical.

作为参考,这里是该问题的接受答案的C FAQ链接,当然这里链接的C99和C11标准是规范的。

#3


1  

Yes, they are, as long they have static or thread storage duration.

是的,他们是,只要他们有静态或线程存储持续时间。

C11 (n1570), § 6.7.9 Initialization #10

C11(n1570),§6.7.9初始化#10

If an object that has static or thread storage duration is not initialized explicitly, then:

如果未显式初始化具有静态或线程存储持续时间的对象,则:

[...]

[...]

  • 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;
  • 如果是聚合,则根据这些规则初始化(递归)每个成员,并将任何填充初始化为零比特;

[...]

[...]

#4


1  

I would add that static variables (or arrays) are classified into two types.

我想补充一点,静态变量(或数组)分为两种类型。

Initialized are the ones that are given value from code at compile time. These are usually stored in DS though this is compiler specific.

初始化是在编译时从代码中获得值的那些。这些通常存储在DS中,尽管这是特定于编译器的。

The other type is uninitialized statics which are initialized at run time and are stored into BSS segment though again this is compiler specific.

另一种类型是未初始化的静态,它们在运行时初始化并存储到BSS段中,但这又是编译器特定的。

BSS

BSS