C++中静态局部变量的生存期是什么?是贯穿整个程序还是从调用该函数开始到整个程序结束?

我想知道它的生存期什么时候开始的

局部静态变量是在函数内的局部作用域的具有静态存储期(static duration)的对象。一个局部静态变量的生存期开始于该变量被初始化时自动构造(此过程中如果有构造函数则调用构造函数)完毕后,结束于整个程序结束时自动销毁(此过程中如果有析构函数则调用析构函数)前一刻。
以下是直接理论依据参考:
标准明确程序终止时自动销毁静态存储期对象:
3.6.3 Termination [basic.start.term]
1 Destructors (12.4) for initialized objects of static storage duration (declared at block scope or at namespace scope) are called as a result of returning from main and as a result of calling exit (18.3). These objects are destroyed in the reverse order of the completion of their constructor or of the completion of their dynamic initialization. If an object is initialized statically, the object is destroyed in the same order as if the object was dynamically initialized. For an object of array or class type, all subobjects of that object are destroyed before any local object with static storage duration initialized during the construction of the subobjects is destroyed.
标准指出静态存储期对象保持到程序结束:
3.7.1 Static storage duration [basic.stc.static]
1 All objects which neither have dynamic storage duration nor are local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3).
关于对象(包括变量)的生存期(lifetime),标准有如下明确定义:
3.8 Object Lifetime [basic.life]
1 The lifetime of an object is a runtime property of the object. The lifetime of an object of type T begins when:
— storage with the proper alignment and size for type T is obtained, and
— if T is a class type with a non-trivial constructor (12.1), the constructor call has completed.
The lifetime of an object of type T ends when:
— if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or
— the storage which the object occupies is reused or released.
关于声明局部的静态存储期对象的初始化的方式(零初始化)和时机(在其它非静态对象初始化之前,一般实现为第一次进入函数时):
6.7 Declaration statement [stmt.dcl]
4 The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any other initialization takes place.
相关概念参考:
关于存储期,标准有如下定义:
3.7 Storage duration [basic.stc]
1 Storage duration is the property of an object that defines the minimum potential lifetime of the storage containing the object. The storage duration is determined by the construct used to create the object and is one of the following:
— static storage duration
— automatic storage duration
— dynamic storage duration
零初始化的定义:
8.5 Initializers [dcl.init]
5 To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;
— if T is a union type, the object’s first named data member89) is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.
关于静态变量的存储,并不完全通过语言控制。标准只规定了静态对象的行为,并没有限制一个实现实际必须怎么存储静态存储期对象。实际的语言实现中,通常给局部静态变量分配的空间会被预先保留,直至程序执行到需要初始化时才访问这些空间,构造结束后局部静态变量的生存期即开始。bluedodo2010 对“生存期”的概念理解有误,注意“分配空间”只是前期的必要条件之一而已。而命名空间作用域的静态存储期对象(例如全局变量)的生存期到确实同程序开始,基本上所有实现中main函数被执行前都会有一段代码来初始化整个程序中用户定义的全局变量以及其它资源(例如标准输出流)。
====
[原创回答团]

参考资料:原创 + ISO/IEC 14882:2003

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-05-07
静态局部变量存在全局数据区,他直到函数结束也不会消失 ,每次重新调用声明静态变量的函数时,也不会为其重新分配空间,它始终保留在全局数据区,直到程序运行结束追问

就是说整个程序开始的时候,静态局部变量的生存期就开始了?

追答

对,程序一开始运行,生存期就开始了,而且它是在默认的数据区中分配空间的。汇编可以看到

本回答被网友采纳
第2个回答  2011-03-19
在程序结束前,他一直存在!在函数的多次调用中,静态的局部对象会持续存在并保存他的值!
例如:
int fun()
{
static int coutnt = 0;
return ++count;
}
int main()
{
fun();
fun();
cout << fun() << endl;结果会输出3
}