确切存储静态变量的位置? [英] where exactly static variables are stored ?

查看:92
本文介绍了确切存储静态变量的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的问题是存储了哪些静态变量?在堆栈或DataSegment?

C#中的静态变量概念是否与c ++相同?

解决方案

请参阅此链接相同讨论

http://stackoverflow.com/questions/93039/where-are-static-变量存储在cc中[ ^ ]



 DATA SEGMENT MEMORY AREA中的所有静态变量存储。 

现在问题是,如果有两个函数包含具有
同名的静态变量,那么编译器如何管理两个var而不会出现任何歧义。

所以请看转换静态变量: -
参见实际解释:让我们有两个函数,即f1和f2如下

void f1(void) {static int iX = 0; }
void f2(void){static int iX = 0; }

在编译这些代码期间,编译器转换名为stag的标记中的变量,并定义如下:
.stabsiX:f1,38,0,0,_iX {表示静态varibale iX函数1}
.stabsiX:f2,38,0,0,_iX {对于函数2的静态varibale iX}

对于每个进程都有一个符号表它实际上包含所有变量和功能映射。

汇编程序将stab转换为`.o'文件中的此符号表条目。该位置表示为数据段偏移量。
00000084 - 00 0000 STSYM iX:f1
00000088 - 00 0000 STSYM iX:f2

符号表是编译时数据结构。静态类型语言在运行时不使用它。形式上,符号表
将名称映射到声明(称为属性),例如将变量名称x映射到其类型int。更确切地说,它为每个类型名称存储:
1),其类型定义(例如,对于C类型声明typedef int * mytype,它将名称
mytype映射到表示类型int *的数据结构)。
2)每个变量名称,其类型。如果变量是数组,它还存储维度信息。它还可以存储
存储类,激活记录中的偏移等。
3)每个常量名称,其类型和值。每个函数和过程,其形式参数列表及其输出类型
4)。每个形式参数必须具有名称,
类型,传递类型(按引用或按值)等。

在可执行文件的符号表条目中,链接器已经创建了可重定位的地址绝对和符号表不在图片中。

0000e0ba - 00 0000 STSYM iX:f1
0000e00c - 00 0000 STSYM iX:f2


C ++ C#静态变量有点不同,例如在 C#编程中语言,你只能使用类成员静态变量( C ++ 允许全局静态变量,以及静态变量 local 到函数)。

有关内存分配的一些详细信息,您可以查看,例如:Static Keyword Demystified [ ^ ]( C#),静态变量在哪里存储(用C / C ++)? [ ^ ]( C ++ )。


MSDN [ ^ ]有答案......

hi,

My question is where exactly static variables are stored ? On stack or DataSegment ?
Is the static variable concept in C# is same as c++ ?

解决方案

Refer this Link Same Discussion
http://stackoverflow.com/questions/93039/where-are-static-variables-stored-in-c-c[^]

The all Static Variables store in DATA SEGMENT MEMORY AREA.

Now Question is that if there are two function contain  static variables having 
same name then how compiler manages two var without falling in any ambiguity.

So see the TRANSFORMATION ON STATIC VARIABLE:-
See the practical explaination :   Lets we have two function namely f1 and f2 as follows
   
   void f1(void)   {     static int iX = 0;   }
   void f2(void)   {     static int iX = 0; }
   
   During compiling of these codes , compiler converts variables in token called stag and defines as follows
   .stabs "iX:f1",38,0,0,_iX   { for the static varibale iX of function 1 }
   .stabs "iX:f2",38,0,0,_iX   { for the static varibale iX of function 2 }
   
    For each process there is a symbol table which actually contains all variables and fuctions map.
    
    The assembler transforms the stab into this symbol table entry in the `.o' file. The location is expressed as a data segment offset. 
     00000084 - 00 0000 STSYM iX :f1
     00000088 - 00 0000 STSYM iX :f2
  
   Symbol table is a compile-time data structure.  It's not used during run time by statically typed languages.  Formally, a symbol table 
   maps names into declarations (called attributes), such as mapping the variable name x to its type int. Moreever it stores :
   1) for each type name, its type definition (eg. for the C type declaration typedef int* mytype, it maps the name
        mytype to a data structure that represents the type int*). 
   2) for each variable name, its type. If the variable is an array, it also stores dimension information. It may also store
       storage class, offset in activation record etc. 
   3)for each constant name, its type and value. 
   4) for each function and procedure, its formal parameter list and its output type. Each formal parameter must have name, 
       type, type of passing (by-reference or by-value), etc. 
       
       In the symbol table entry from the executable, the linker has made the relocatable address absolute and symbol table goes out of picture.
       
        0000e0ba - 00 0000 STSYM iX :f1
        0000e00c - 00 0000 STSYM iX :f2


C++ and C# static variables are a bit different, for instance in C# programming language, you can use only class member static variables (C++ allows global static variables, as well static variables local to a function).
For some details on memory allocation you may look, for instance: "Static Keyword Demystified"[^] (C#), Where are static variables stored (in C/C++)?[^] (C++).


MSDN[^] has its answer...


这篇关于确切存储静态变量的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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