C语言 C中静态变量的初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13251083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The initialization of static variables in C
提问by pmg
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?
回答by pmg
Yes, all members are initialized for objects with static storage. See 6.7.8/10 in the C99 Standard (PDF document)
是的,所有成员都针对具有静态存储的对象进行了初始化。参见C99 标准(PDF 文档)中的 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.
如果没有显式初始化具有自动存储持续时间的对象,则其值是不确定的。如果没有显式初始化具有静态存储持续时间的对象,则:
— 如果它具有指针类型,则将其初始化为空指针;
——如果它有算术类型,它被初始化为(正或无符号)零;
— 如果是聚合,则根据这些规则(递归地)初始化每个成员;
— 如果是联合,则根据这些规则(递归地)初始化第一个命名成员。
To initialize everything in an object, whether it's staticor not, to 0, I like to use the universal zero initializer
要将对象中的所有内容(无论是否static为 0)初始化为 0,我喜欢使用通用零初始化程序
sometype identifier0 = {0};
someothertype identifier1[SOMESIZE] = {0};
anytype identifier2[SIZE1][SIZE2][SIZE3] = {0};
There is no partial initializationin C. An object either is fully initialized (to 0of the right kind in the absence of a different value) or not initialized at all.
If you want partial initialization, you can't initialize to begin with.
C 中没有部分初始化。一个对象要么完全初始化(0在没有不同值的情况下是正确的类型),要么根本没有初始化。
如果你想要部分初始化,你不能初始化开始。
int a[2]; // uninitialized
int b[2] = {42}; // b[0] == 42; b[1] == 0;
a[0] = -1; // reading a[1] invokes UB
回答by md5
Yes, they are, as long they have static or thread storage duration.
是的,它们是,只要它们具有静态或线程存储持续时间。
C11 (n1570), § 6.7.9 Initialization #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;
[...]
C11 (n1570), § 6.7.9 初始化 #10
如果没有显式初始化具有静态或线程存储持续时间的对象,则:
[...]
- 如果它有算术类型,则初始化为(正或无符号)零;
- 如果是聚合,则根据这些规则(递归地)初始化每个成员,并将任何填充初始化为零位;
[...]
回答by Useless
Yes, file-scope static variables are initialized to zero, including all members of structures, arrays, etc.
是的,文件范围的静态变量被初始化为零,包括结构、数组等的所有成员。
See this questionfor 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 thatquestion as a duplicate of this, instead.
编辑:这个问题得到了更好的答案,所以我投票结束这个问题作为this的副本,而不是。
For reference, here is the C FAQ linkfrom that question's accepted answer, although of course the C99 and C11 standards linked here are canonical.
作为参考,这里是该问题已接受答案的C FAQ 链接,当然这里链接的 C99 和 C11 标准是规范的。
回答by fkl
I would add that static variables (or arrays) are classified into two types.
我想补充一点,静态变量(或数组)分为两种类型。
Initializedare the ones that are given value from code at compile time. These are usually stored in DS though this is compiler specific.
Initialized是在编译时从代码中给定值的那些。这些通常存储在 DS 中,尽管这是特定于编译器的。
The other type is uninitializedstatics which are initialized at run time and are stored into BSS segment though again this is compiler specific.
另一种类型是未初始化的静态变量,它们在运行时初始化并存储到 BSS 段中,尽管这也是特定于编译器的。

