为什么初始化一个全局变量,返回一个函数的返回值时声明失败,但在文件范围内工作正常? [英] Why's initializing a global variable with return value of a function failing at declaration,but works fine at file scope?

查看:326
本文介绍了为什么初始化一个全局变量,返回一个函数的返回值时声明失败,但在文件范围内工作正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

80k 声誉贡献者 R .. 告诉我,我们无法用函数的返回值初始化全局变量这不被认为是一个常量,而且全局变量必须用一个常量初始化。并且按照他的话来说,我会像预期的那样得到以下错误 - 初始化元素不是常量。这里是程序:

  #include< stdio.h> 
int foo();
int gvar = foo(); //错误

int main()
{
printf(%d,gvar);
}

int foo()
{
return 8;
}

但是在这种情况下,我只是不明白为什么后面的修改版本上面的程序完全没有显示任何错误,并且工作正常。在第二个程序中,我使用相同函数 foo()的返回值初始化相同的全局变量.你能告诉我结果变化的严格技术原因是什么?为什么在函数的返回值导致错误时初始化全局变量,但是具有相同返回值的相同初始化在一个函数内完成时工作正常吗?

  #include  
int foo();
int gvar;

int main()
{
gvar = foo();
printf(%d,gvar);
}

int foo()
{
return 8;
}

输出 8

解决方案

背后的原因是,为了确定函数产生的值,需要执行代码,并且初始化静态变量和全局变量时,C中不会执行代码执行。编译器和链接器一起工作以准备全局内存段的字节映像:编译器提供这些值,并且链接器执行它们的最终布局。在运行时,段的图像按原样加载到内存中,无需进一步修改。这发生在任何代码被执行之前,所以不能进行函数调用。



请注意,这并不意味着某些技术原因是不可能的,只有C设计师决定不这样做。例如,C ++编译器生成一个代码段,调用全局对象的构造函数,在控制传递给main()之前执行全局对象。


An 80k reputation contributor R.. told me on SO that we can't initialize global variables with the return value of a function as that's not considered a constant,and global variables must be initialized with a constant.And true to his words,I get the following error for this program as expected-- initializer element is not a constant.Here is the program:

#include<stdio.h>
int foo();
int gvar=foo();  //ERROR

int main()
{
printf("%d",gvar);
}

int foo()
{
 return 8;
}

But in this context,I just don't understand why the followed altered version of the above program shows no error at all and works fine.In this second program,I am initializing the same global variable with the return value of the same function foo().Can you tell me what is the rigorous technical reason for this variation in results?Why is initializing the global variable with the return value of a function at it's declaration causing error but the same initialization with the same return value works fine when done from within a function?

#include<stdio.h>
int foo();
int gvar;

int main()
{
gvar=foo();
printf("%d",gvar);
}

int foo()
{
return 8;
}

Output 8

解决方案

The reason behind it is that in order to determine a value produced by a function one needs to execute code, and that there is no code execution done in C when initializing static and global variables.

Compiler and linker work together to prepare a byte image of the global memory segment: the compiler provides the values, and the linker performs their final layout. At runtime, the image of the segment is loaded in memory as is, without further modifications. This happens before any code gets executed, so no function calls can be made.

Note that this does not mean that it is not possible for some technical reason, only that C designers decided against doing it. For example, C++ compiler generates a code segment that calls constructors of global objects, which gets executed before the control is passed to main().

这篇关于为什么初始化一个全局变量,返回一个函数的返回值时声明失败,但在文件范围内工作正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆