Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question on linkage

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
when a variable is defined as both extern and static

eg

//file1.cpp

extern int i;
static int i=30;

void main()
{
//the value is 30

}

whats the reason.

What i understand is that since the variable i is now a having internal likange. So, the linker first sees if the
variable is there in the same file in which it is defined.
As it finds it it prints that value. Otherwise it would
have searched in other files. I am not sure if this
is a correct explaination. Please help?

hawapani
 
Maybe a simpler way to understand this is to first use a static variable inside a function.
Static values are used to maintain their value between function calls.

My understanting of this is that the memory allocated for the static variable is fixed throughout the program, so it will hold its value when re-entering the function.

This explanation can be expanded then to the different modules(source file, in your case) in a program.

Because of the fixed memory stuff, it is not a good idea to use these variables extensively, nor have huge static structure or arrays in a program.

HTH... [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 

extern int i;
is actual a message for the compiler/linker, telling it
when 'seeing i' while processing this file, not the be alarmed when running into it; it is in another file you know about so its not undeclared identifier.
It guess the linker may save an entry for i in its tables when it runs into an occurence, but will have postpone resolving it till the actuall declaration is found. (I don't know).
If it cannot be found in any of the files when the linking is complete you'd probably end up with an 'external symbol not found1333##$@!!%&^^!' error.

 
My doubt is not the what my learned fried Nosferatu has understood.

say a given variable is
said to be extern so its like we are declaring it ( not defining hence no memory allocation) now if we declare and define the same variable in the same file, everything works
fine.

Now what i understand is this. After compilation, during
link time the varible is first searched in the same file where it is declared, if it is not found and the linker knows that it has to be relsoved externally so it searches in other files and we get the required value. In our case
thought the variable is declared as extern at the same time defined in the same file, so the linker as it searches the varible in the same file it finds the reference correctly.
Otherwise it would have looked in other files

Is this explaination correct? OR AM I MISSING SOMETHING

HELP ME PLEASE
hawapani
 
In the original post, where i is defined with both internal and external linkage, the behavior is undefined. Therefore there is no way of predicting how a compiler will handle it. Here's the supporting quote from C99:

6.2.2(7):

If, within a translation unit, the same identifier appears with both internal and external linkage, the behavior is undefined.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top