2

我想知道编译器内部发生了什么......比如它是否将全局变量存储在不同的位置。

4

3 回答 3

3

符号表上的维基百科页面可以为您提供一个基本的了解。

http://en.wikipedia.org/wiki/Symbol_table

在计算机科学中,符号表是语言翻译器(例如编译器或解释器)使用的数据结构,其中程序源代码中的每个标识符都与与其在源代码中的声明或外观有关的信息相关联,例如其类型,范围级别,有时还有它的位置。

[...]

一种常见的实现技术是使用哈希表实现。编译器可以为所有符号使用一个大符号表,或者为不同的范围使用分离的、分层的符号表。

强调我的。

于 2012-11-12T11:09:29.800 回答
0

它知道变量是全局的还是局部的,通过你声明它的方式。

//declared at namespace scope - global
extern int x;     

int main()
{
   //declared inside a method - local
   int y;
};
于 2012-11-12T11:07:54.860 回答
0

通常任何变量都有 4 个作用域。使用extern关键字,您正在显式地创建该变量。(默认情况下,全局变量是extern

使用static将变量或函数范围限制为当前文件

据此,内存被分配在不同的段中

        global: visible in more than one source file
 -- data segement(also differs whether initialised or uninitialized)


        local : visible with in { } it also called function(){} scope
 -- on stack 


        block : {} inside any function another scope of block valiables with in {}

     -- on stack if with in function 


        file : making static variable is limited to it's file scope or 
    current translation unit. -- again data section
于 2012-11-12T11:13:02.613 回答