Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 5-Dec-12 2:07am
rajenh410
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
        
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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++).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

MSDN[^] has its answer...
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 530
1 OriginalGriff 379
2 Manas Bhardwaj 350
3 Abhinav S 295
4 Sampath Lokuge 255
0 Sergey Alexandrovich Kryukov 7,647
1 OriginalGriff 4,287
2 Peter Leow 3,681
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 5 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid