生存期Lifetime

“生存期”是其中存在变量或函数的程序执行的时段。"Lifetime" is the period during execution of a program in which a variable or function exists. 标识符的存储持续时间决定其生存期。The storage duration of the identifier determines its lifetime.

使用 storage-class-specifier static 声明的标识符具有静态存储持续时间。An identifier declared with the storage-class-specifier static has static storage duration. 具有静态存储持续时间的标识符(也称为“全局”)具有存储和程序持续时间的定义值。Identifiers with static storage duration (also called "global") have storage and a defined value for the duration of a program. 将保留存储,并且在程序启动前只将标识符的存储值初始化一次。Storage is reserved and the identifier's stored value is initialized only once, before program startup. 使用外部或内部链接声明的标识符还具有静态存储持续时间(请参阅链接)。An identifier declared with external or internal linkage also has static storage duration (see Linkage).

如果在函数内部声明未使用 static 存储类说明符声明的标识符,则该标识符将具有自动存储持续时间。An identifier declared without the static storage-class specifier has automatic storage duration if it is declared inside a function. 具有自动存储持续时间的标识符(“本地标识符”)具有存储和已定义的值(仅在定义或声明该标识符的块中)。An identifier with automatic storage duration (a "local identifier") has storage and a defined value only within the block where the identifier is defined or declared. 程序每次进入该块时都将为自动标识符分配新存储,并在程序退出该块时丢失其存储(及其值)。An automatic identifier is allocated new storage each time the program enters that block, and it loses its storage (and its value) when the program exits the block. 函数中声明的不具有链接的标识符也具有自动存储持续时间。Identifiers declared in a function with no linkage also have automatic storage duration.

以下规则指定标识符是具有全局(静态)还是局部(自动)生存期:The following rules specify whether an identifier has global (static) or local (automatic) lifetime:

  • 所有函数都具有静态生存期。All functions have static lifetime. 因此,它们在程序执行期间始终存在。Therefore they exist at all times during program execution. 在外部级别(即,在同一级别函数定义的程序中的所有块之外)声明的标识符始终具有全局(静态)生存期。Identifiers declared at the external level (that is, outside all blocks in the program at the same level of function definitions) always have global (static) lifetimes.

  • 如果局部变量具有初始值设定项,则会在每次创建该变量时对其进行初始化(除非它被声明为 static)。If a local variable has an initializer, the variable is initialized each time it is created (unless it is declared as static). 函数参数也具有本地生存期。Function parameters also have local lifetime. 可以通过在标识符声明中包含 static 存储类说明符,在块中为该标识符指定全局生存期。You can specify global lifetime for an identifier within a block by including the static storage-class specifier in its declaration. 一旦声明 static,该变量会将其值从块中的一个项保留到下一个项中。Once declared static, the variable retains its value from one entry of the block to the next.

    尽管源程序(例如,外部声明的变量或使用 static 关键字声明的局部变量)的执行中存在具有全局生存期的标识符,它也不会在程序的所有部分中可见。Although an identifier with a global lifetime exists throughout the execution of the source program (for example, an externally declared variable or a local variable declared with the static keyword), it may not be visible in all parts of the program. 有关可见性的信息,请参阅范围和可见性;有关 storage-class-specifier 非终止符的讨论,请参阅存储类See Scope and Visibility for information about visibility, and see Storage Classes for a discussion of the storage-class-specifier nonterminal.

    如果通过使用特殊库例程(如 malloc)创建,则可以根据需要分配内存(动态)。Memory can be allocated as needed (dynamic) if created through the use of special library routines such as malloc. 由于动态内存分配使用库例程,因此它不被视为语言的一部分。Since dynamic memory allocation uses library routines, it is not considered part of the language. 请参阅《运行时库参考》中的 malloc 函数。See the malloc function in the Run-Time Library Reference.

另请参阅See Also

生存期、范围、可见性和链接Lifetime, Scope, Visibility, and Linkage