关于全局变量
如果定义了一个整型数组,并且数组是局部变量,那么在不初始化的情况下,数组里的元素是不确定的。
可是为什么把这个数组定义为全局变量,即使不人为地初始化,系统也会自动把所有元素赋值0呢?
这仅是整型数组,那么字符型、、以及其他类型呢?
请高手请教,最好能告诉我较为详细的原因,谢谢!
[解决办法]
在括号体里{}定义的是局部变量,它的生命周期是该块结束。局部变量需要自己初始化,并且位于堆栈中。
如果用static前缀,如在源文件开头的话是本原文件的全局变量,如果在所有函数前定义不加static的话就是全局变量。 --- 这个静态的非常复杂,静态全局和全局和局部都不一样,静态全局实际上生命周期不同,和全局变量类似,它的生命周期和文件无关,跨文件访问也是正确的,只是改变了可见性,即你不能直接在其他文件中看见。
对于全局变量,不是static的,也不在任何块内。生命周期是整个程序的运行过程,可见性也是整个程序。extern的作用仅仅是告诉编译器,那个变量的类型,要不然编译器不认识那个变量。所以同一个文件里,extern 和 不带extern的声明可以同时存在。
[解决办法]
11.1.4. Declarations and Definitions
You can declare an identifier as often as you want, but only one declaration within its scope
can be a definition. Placing the definitions of objects and functions with external linkage in
header files is a common way of introducing duplicate definitions, and is therefore not a good
idea.
An identifier's declaration is a definition in the following cases:
? A function declaration is a definition if it contains the function block. An example:
?
int iMax( int a, int b ); // This is a declaration, not a
definition.
? int iMax( int a, int b ) // This is the function's definition.
{ return ( a >= b ? a : b ); }
? An object declaration is a definition if it allocates storage for the object. Declarations
that include initializers are always definitions. Furthermore, all declarations within
function blocks are definitions unless they contain the storage class specifier
extern
.
Some examples:
Page 198
?
? int a = 10; // Definition of a.
? extern double b[ ]; // Declaration of the array b, which is
? // defined elsewhere in the program.
? void func( )
? {
? extern char c; // Declaration of c, not a definition.
? static short d; // Definition of d.
? float e; // Definition of e.
? /* ... */
}
If you declare an object outside of all functions, without an initializer, and without the
storage class specifier
extern
, the declaration is a tentative definition . Som e examples:
int i, v[ ]; // Tentative definitions of i, v and j.
static int j;
A tentative definition of an identifier remains a simple declaration if the translation unit
contains another definition for the same identifier. If not, then the compiler behaves as
if the tentative definition had included an initializer with the value zero, m aking it a
definition. Thus the
int
variables
i
and
j in the previous exam ple, whose identifiers are
declared without initializers, are implicitly initialized with the value 0, and the
int
array
v has one element, with the initial value 0.
C语言核心技术第中文版第十一章,“定义和声明”这一节解释的很到位,你可以看看,第163页
[解决办法]
11.1.4. Declarations and Definitions
You can declare an identifier as often as you want, but only one declaration within its scope
can be a definition. Placing the definitions of objects and functions with external linkage in
header files is a common way of introducing duplicate definitions, and is therefore not a good
idea.
An identifier's declaration is a definition in the following cases:
? A function declaration is a definition if it contains the function block. An example:
?
int iMax( int a, int b ); // This is a declaration, not a
definition.
? int iMax( int a, int b ) // This is the function's definition.
{ return ( a >= b ? a : b ); }
? An object declaration is a definition if it allocates storage for the object. Declarations
that include initializers are always definitions. Furthermore, all declarations within
function blocks are definitions unless they contain the storage class specifier
extern
.
Some examples:
Page 198
?
? int a = 10; // Definition of a.
? extern double b[ ]; // Declaration of the array b, which is
? // defined elsewhere in the program.
? void func( )
? {
? extern char c; // Declaration of c, not a definition.
? static short d; // Definition of d.
? float e; // Definition of e.
? /* ... */
}
If you declare an object outside of all functions, without an initializer, and without the
storage class specifier
extern
, the declaration is a tentative definition . Som e examples:
int i, v[ ]; // Tentative definitions of i, v and j.
static int j;
A tentative definition of an identifier remains a simple declaration if the translation unit
contains another definition for the same identifier. If not, then the compiler behaves as
if the tentative definition had included an initializer with the value zero, m aking it a
definition. Thus the
int
variables
i
and
j in the previous exam ple, whose identifiers are
declared without initializers, are implicitly initialized with the value 0, and the
int
array
v has one element, with the initial value 0.
C语言核心技术第中文版第十一章,“定义和声明”这一节解释的很到位,你可以看看,第163页
[解决办法]
没有初始化的全局变量会自动清零
不限类型,无论字符还是整形
参考 浅谈全局变量初始值问题
[解决办法]
http://www.nongnu.org/avr-libc/user-manual/mem_sections.html
结合5楼就明白为什么了。
[解决办法]
首先你在函数内定义的数组和在函数外面定义的数组存在内存不同的地方;内部定义的存在栈上,外部定义的存在数据段上。存放在数据段上的全局变量,系统会给其初始化为0,如果你没有对其初始化的话。这也算系统默认的吧。
[解决办法]
1. 任何变量使用之前都需要初始化。
2. 因为全局变量在main函数之前就存在了,而且在到处可见,但编译器不知道在什么地方、什么时候会用到这些全局变量,所以就给它们一个初始值了,这样就可以避免在使用全局变量之前,全局变量还没有被初始化的情形。
[解决办法]
规定吧!