How is static variable stored in the memory?( if there are 2 functions in a file,and the static variable name is same (ex var) in both the function. how is it keep seperately in the memory).
static varibles storage is memory not register and default value is 0.they are initialized only once.
Siva Nookala
Apr 8th, 2006
C++ uses name mangling when storing both local and global static varibales at the same place. The local static variables have function name and the global variables will have file name. Essentially the compiler uses namespace to distinguish between local and global static variables.
Nilesh roy
Apr 13th, 2006
static variables are stored in memory and not in registers and its default value is 0 also they are initialized only once.
Munish
Apr 14th, 2006
Static variables are stored in Datasegment in the memory. Thier life time is till the program terminates. but their scope is local to the function in which they are declared. if declared global then their scope is limited to the file in which they are declared.
Sameer
Apr 19th, 2006
static variables are stored in static memory location, like HEAP
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
NOTE :- THIS IS GENERAL CONCEPT , MAY THIS THINGS VARY FROM COMPILER TO COMPILER
IF YOU PEOPLE HAVE GOOD CLARIFICATION PLEASE SEND ME ON :
Wanted to explain the later part of the quesiton, when there are two static variables with the same name in two files then what would happen?
To be clear lets undertand what the term static means in various scenarios,
(I)When a local variable is made static in a function, it can be initialized only once and it exists thru out to scope of the function.
(II)If a global variable or a function is made static, it means that that particular variable/function is local to that file.(If u try to access that variable/function from other file using the word ?extern?, it would result in an error).
(III)The static variables declared inside a class is the third type.
As the (II) explains clearly, the static variable is local to that file. So though there exists a staic variable with the same name, it doesnt matter.
Herojeet
Nov 7th, 2007
Even if the static local variables residing in different functions have the SAME name, during compilation, by a process called "name mangling" the static variable names are modified by adding some part of the function it belongs. So after this there no longer exist two variables of same name thus avoiding the ambiguity.
Sorry Sameer, but heap is used only when new, malloc, HeapAlloc, or similar methodes are used; local variables are stores in stack, and static and global varibales are stored in the memory space where the data section of the executable (EXE or DLL or whatever) is loaded. This has two types, initialized and not-initialized data sections. When you write int x; the space will be declared in the EXE file but not really allocated till the file is loaded into memory. But when you write int x = 5; the space is really allocated in the EXE file with the value 5 and copied to the memory when loaded. On the other hand, local variables have nothing to do with the EXE file, they are only allocated on the processor stack and initialized at runtime.
why u people make thing so complex yaar... its very simple we have 5 type of storage in c++ 1)const storage .........contain const veriable,...can be local or global ....in c++ const linkage is internal....but in c it is external
2)heap storage ..........if we use new ..it will allocate memory in heap
3)free storage if we use malloc it will use free starage(some time heap & free starage..used interchangebly.....but velieve me its difrent...but both r dynamic)
4)global static storage
.......global variable & static (local or global) stored in this area it contain two section..intialize & uninitialize those variable that have the value will fall in intialize & rest r in unintialize part ...then assigned value 0.
it doesnt keep separate memory for static variables.common memory is shared by the static variables .
Ramanuj
Oct 20th, 2012
Hi,
I have a query. The lifetime of the static variable is the entire program - if it is a local auto variable (i.e., not dynamically allocated), will it contribute to the peak memory ?
Regards,
Ramanu
Bishoy
Oct 20th, 2012
Local variables are stored in stack - each method have a stack frame (a fixed size of memory in the stack) that is allocated for local variables each time the method is called, and deallocated when the method return. The deallocated stack memory is used by the next method being called, thats why it is not a good idea to return a pointer to a local variable as a return of a method.
How is static variable stored in the memory?( if there are 2 functions in a file,and the static variable name is same (ex var) in both the function. how is it keep seperately in the memory).
Questions by Subhajit
Related Answered Questions
Related Open Questions