Possible Duplicate:
Why are global and static variables initialized to their default values?可能重复:为什么全局变量和静态变量被初始化为默认值?
See the code,
看代码,
#include <stdio.h>
int a;
int main(void)
{
int i;
printf("%d %d\n", a, i);
}
Output
输出
0 8683508
Here 'a' is initialized with '0', but 'i' is initialized with a 'junk value'. Why?
这里“a”初始化为“0”,而“i”初始化为“垃圾值”。为什么?
4 个解决方案
#1
91
Because that's the way it is, according to the C Standard. The reason for that is efficiency:
根据C标准,这就是它的方式。原因是效率:
-
static variables are initialized at compile-time, since their address is known and fixed. Initializing them to
0
does not incur a runtime cost.静态变量在编译时初始化,因为它们的地址是已知的和固定的。将它们初始化为0不会导致运行时成本。
-
automatic variables can have different addresses for different calls and would have to be initialized at runtime each time the function is called, incurring a runtime cost that may not be needed. If you do need that initialization, then request it.
自动变量可以对不同的调用有不同的地址,并且在每次调用函数时都必须初始化,从而产生可能不需要的运行时成本。如果确实需要初始化,那么请求它。
#2
33
global
and static
variables are stored in the Data Segment (DS) when initialized and block start by symbol (BSS)` when uninitialized.
全局变量和静态变量都存储在数据段(DS)中,当初始化并在未初始化时以符号(BSS)开头。
These variables have a fixed memory location, and memory is allocated at compile time.
这些变量有一个固定的内存位置,并且在编译时分配内存。
Thus global
and static
variables have '0'
as their default values.
因此,全局变量和静态变量的默认值为“0”。
Whereas auto
variables are stored on the stack, and they do not have a fixed memory location.
而自动变量存储在堆栈中,并且它们没有固定的内存位置。
Memory is allocated to auto
variables at runtime, but not at compile time. Hence auto
variables have their default value as garbage.
内存在运行时分配给自动变量,而不是在编译时。因此自动变量的默认值是垃圾。
#3
13
You've chosen simple variables, but consider:
你选择了一些简单的变量,但考虑一下:
void matrix_manipulation(void)
{
int matrix1[100][100];
int matrix2[100][100];
int matrix3[100][100];
/* code to read values for matrix1 from a file */
/* code to read values for matrix2 from a file */
/* code to multiply matrix1 by matrix2 storing the result in matrix3 */
/* code to use matrix3 somehow */
}
If the system initialized the arrays to 0, the effort would be wasted; the initialization is overwritten by the rest of the function. C avoids hidden costs whenever possible.
如果系统初始化数组为0,那么工作将被浪费;初始化由函数的其余部分覆盖。尽可能避免隐性成本。
#4
7
Global variables are allocated and initialized before the main
function starts, while local variables are generated on the stack as the instance of the program runs.
全局变量在主函数开始之前被分配和初始化,而本地变量作为程序运行的实例在堆栈上生成。
#1
91
Because that's the way it is, according to the C Standard. The reason for that is efficiency:
根据C标准,这就是它的方式。原因是效率:
-
static variables are initialized at compile-time, since their address is known and fixed. Initializing them to
0
does not incur a runtime cost.静态变量在编译时初始化,因为它们的地址是已知的和固定的。将它们初始化为0不会导致运行时成本。
-
automatic variables can have different addresses for different calls and would have to be initialized at runtime each time the function is called, incurring a runtime cost that may not be needed. If you do need that initialization, then request it.
自动变量可以对不同的调用有不同的地址,并且在每次调用函数时都必须初始化,从而产生可能不需要的运行时成本。如果确实需要初始化,那么请求它。
#2
33
global
and static
variables are stored in the Data Segment (DS) when initialized and block start by symbol (BSS)` when uninitialized.
全局变量和静态变量都存储在数据段(DS)中,当初始化并在未初始化时以符号(BSS)开头。
These variables have a fixed memory location, and memory is allocated at compile time.
这些变量有一个固定的内存位置,并且在编译时分配内存。
Thus global
and static
variables have '0'
as their default values.
因此,全局变量和静态变量的默认值为“0”。
Whereas auto
variables are stored on the stack, and they do not have a fixed memory location.
而自动变量存储在堆栈中,并且它们没有固定的内存位置。
Memory is allocated to auto
variables at runtime, but not at compile time. Hence auto
variables have their default value as garbage.
内存在运行时分配给自动变量,而不是在编译时。因此自动变量的默认值是垃圾。
#3
13
You've chosen simple variables, but consider:
你选择了一些简单的变量,但考虑一下:
void matrix_manipulation(void)
{
int matrix1[100][100];
int matrix2[100][100];
int matrix3[100][100];
/* code to read values for matrix1 from a file */
/* code to read values for matrix2 from a file */
/* code to multiply matrix1 by matrix2 storing the result in matrix3 */
/* code to use matrix3 somehow */
}
If the system initialized the arrays to 0, the effort would be wasted; the initialization is overwritten by the rest of the function. C avoids hidden costs whenever possible.
如果系统初始化数组为0,那么工作将被浪费;初始化由函数的其余部分覆盖。尽可能避免隐性成本。
#4
7
Global variables are allocated and initialized before the main
function starts, while local variables are generated on the stack as the instance of the program runs.
全局变量在主函数开始之前被分配和初始化,而本地变量作为程序运行的实例在堆栈上生成。