简体   繁体   English

是否保证全局变量始终用c99初始化为0?

[英]Is it guaranteed that global variables are always initialized to 0 with c99?

From 6.7.8.10 in the C99 standard: 从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. - 如果它是一个联合,则根据这些规则初始化(递归)第一个命名成员。

Is a global variable of any type (array, structure, bitfield) always defined as a static storage ? 是任何类型的全局变量(数组,结构,位域)始终定义为static storage

Is it guaranteed that global variables are always initialized to 0 with c99? 是否保证全局变量始终用c99初始化为0?

Yes and no 是的,不是

static void *g_v_ptr;  // initialized to a null pointer

C99 details a value , but not its representation. C99详细说明了一个 ,但不是它的表示。 "it has pointer type, it is initialized to a null pointer" implies that the pointer has value of a null pointer . “它有指针类型,它被初始化为空指针”意味着指针具有空指针的值。 This may or may not be NULL . 这可能是也可能不是NULL This may or may not be 0 . 这可能是也可能不是0 A compiler may have many bit patterns that correspond to a null pointer . 编译器可以具有许多与空指针对应的位模式。 In any case, the g_v_ptr == NULL is true as well as g_v_ptr == 0 , but the pointer may have a different bit representation than 0 . 在任何情况下, g_v_ptr == NULL为真以及g_v_ptr == 0 ,但指针可能具有与0不同的位表示。 Certainly a pattern of all zero bits is usually easy to implement and certainly the most likely implementation. 当然,所有零位的模式通常易于实现,当然也是最可能的实现。 Yet the spec is just squishy enough to allow for some non-zero bit pattern to be used. 然而,规范只是软弱到足以允许使用一些非零位模式。

A similar case can be made for floating point numbers. 可以对浮点数进行类似的情况。

In any case (IAC), the initialized value will equate to 0. 在任何情况下(IAC),初始化值将等于 0。

According to the C Standard (5.1.2 Execution environments) 根据C标准(5.1.2执行环境)

1 Tw o execution environments are defined: freestanding and hosted. 1定义执行环境:独立和托管。 In both cases, program startup occurs when a designated C function is called by the execution environment. 在这两种情况下,当执行环境调用指定的C函数时,程序启动发生。 All objects with static storage duration shall be initialized (set to their initial values) before program startup. 具有静态存储持续时间的所有对象应在程序启动之前初始化(设置为其初始值)。 The manner and timing of such initialization are otherwise unspecified. 否则,这种初始化的方式和时间是不确定的。 Program termination returns control to the execution environment. 程序终止将控制权返回给执行环境。

and (6.2.4 Storage durations of objects) (6.2.4对象的存储持续时间)

3 An object whose identifier is declared without the storage-class specifier _Thread_local, and either with external or internal linkage or with the storage-class specifier static, has static storage duration. 3一个对象的标识符在没有存储类指定_Thread_local的情况下声明,并且具有外部或内部链接或存储类指定静态,具有静态存储持续时间。 Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup. 它的生命周期是程序的整个执行,它的存储值只在程序启动之前初始化一次。

Yes. 是。 It's guaranteed (at least since C89). 这是有保证的(至少从C89开始)。 "Global variables" (either with internal or external linkage) have static storage duration . “全局变量”(具有内部或外部链接)具有静态存储持续时间 Any object with static storage is guaranteed to be zero initialized according to C99, 6.7.8 Initialization. 根据C99,6.7.8初始化,任何具有静态存储的对象都保证初始化为零。

C99 draft, 6.2.4 Storage durations of objects : C99草案,6.2.4对象的存储持续时间

3 An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. 3使用外部或内部链接声明标识符的对象,或者使用存储类说明符static声明的对象具有静态存储持续时间。 Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup. 它的生命周期是程序的整个执行,它的存储值只在程序启动之前初始化一次。

6.2.2 Linkages of identifiers describes the linkage of identifiers, particularly in relation to "global" variables: 6.2.2标识符的链接描述了标识符的链接,特别是与“全局”变量的关联:

3 If the declaration of a file scope identifier for an object or a function contains the storage- class specifier static, the identifier has internal linkage.22) 3如果对象或函数的文件范围标识符的声明包含静态的存储类说明符,则标识符具有内部链接.22)

4 For an identifier declared with the storage-class specifier extern in a scope in which a prior declaration of that identifier is visible,23) if the prior declaration specifies internal or external linkage, the linkage of the identifier at the later declaration is the same as the linkage specified at the prior declaration. 4对于在该标识符的先前声明可见的范围内使用存储类说明符extern声明的标识符,23)如果先前声明指定内部或外部链接,则后面声明中标识符的链接是相同的作为先前声明中指定的联系。 If no prior declaration is visible, or if the prior declaration specifies no linkage, then the identifier has external linkage. 如果没有先前声明可见,或者先前声明未指定链接,则标识符具有外部链接。

Ignoring the case which could lead to undefined behaviour , all file scope identifiers with either internal or external linkage and they all have static storage duration. 忽略可能导致未定义行为的情况 ,所有文件范围标识符都具有内部或外部链接,并且它们都具有静态存储持续时间。

Yes, as far as they are stored into bss section. 是的,只要它们存储在bss部分中。

Your linker script define it and (mainly) default linker scripts do that. 您的链接描述文件定义它并且(主要)默认链接描述文件执行此操作 While you can manually store data in different section manually, global scoped variable too. 虽然您可以手动手动存储不同部分的数据,但也可以手动存储全局范围的变量

BTW is the startup code that is responsible of zero the section. BTW是启动代码,负责将该部分归零。 If you are working with non standard platforms or your startup code is made by you, you must ensure that. 如果您使用的是非标准平台,或者您的启动代码是由您制作的,则必须确保这一点。

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

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