Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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++ ?
Posted

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++).
 
Share this answer
 
MSDN[^] has its answer...
 
Share this answer
 
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900