GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 74 of 203    Print  
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).

  
Total Answers and Comments: 13 Last Update: February 02, 2008     Asked by: Subhajit 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
April 07, 2006 04:54:48   #1  
mahesh kumar sharma        

RE: how is static variable stored in the memory?...
static varibles storage is memory not register and default value is 0.they are initialized only once.
 
Is this answer useful? Yes | No
April 08, 2006 13:37:20   #2  
Siva Nookala        

RE: how is static variable stored in the memory?...
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.
 
Is this answer useful? Yes | No
April 13, 2006 03:34:06   #3  
Nilesh roy        

RE: how is static variable stored in the memory?...

static variables are stored in memory and not in registers and its default value is 0 also they are initialized only once.


 
Is this answer useful? Yes | No
April 14, 2006 02:39:57   #4  
Munish        

RE: how is static variable stored in the memory?...
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.
 
Is this answer useful? Yes | No
April 19, 2006 03:38:36   #5  
Sameer        

RE: how is static variable stored in the memory?...

static variables are stored in static memory location like HEAP


 
Is this answer useful? Yes | No
April 20, 2006 08:13:29   #6  
mohit12379 Member Since: March 2006   Contribution: 17    

RE: how is static variable stored in the memory?...

HI SUBHAJIT

FOLLOWING EXPLAINATION MAY HELP YOU

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 :

mohit.gonduley@gmail.com


 
Is this answer useful? Yes | No
May 08, 2006 21:28:17   #7  
neetu        

RE: how is static variable stored in the memory?...
its not from heap but data segment
 
Is this answer useful? Yes | No
June 02, 2006 06:06:58   #8  
Arunangshu saha        

RE: how is static variable stored in the memory?...
HEAP is not static memory......
 
Is this answer useful? Yes | No
October 10, 2006 10:26:03   #9  
aparna        

RE: how is static variable stored in the memory?...

Hi

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.


 
Is this answer useful? Yes | No
November 07, 2007 04:35:08   #10  
Herojeet        

RE: how is static variable stored in the memory?...
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.
 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape