Differences between extern and static variables.

Apr 3, 2012 at 10:15am
Hi,

What's the difference betwen an extern and a static value? What's best used? Or it's better in case of extern, only defining an global variable and including like a library file.

Thanks a lot.
Regards,
Maria
Apr 3, 2012 at 11:43am
static is not the "opposite" of extern. static variables have very special properties. There are different ways to use a static variables:

1- In class defintions: static variables have a single value along all the instantiations of the class, and do not require an object of a class to be used, and btw, they are initialised automatically, so you don't have to worry about initialising them. For example:

1
2
3
4
class MyClass
{
   static int x;
};


Now to access x, you don't have to have an object of MyClass, you can simply do this:

 
MyClass::x = 5;


You can use static variable in classes, for example, to count how many instances of a class are present.

And not to mention, that static methods in a class can be called without instantiating a class object, as well.

2- In functions: static variables in functions do not depend on the function scope. This means, if you call a function once, and you call it again, the same value of the static variable will remain unchanged.

In general, static means the variable is independent of every scope in the program. This is very useful when you wanna avoid using global variables (which is really a bad style compared to static variables and functions).

extern is simply another name for global variables.
Apr 3, 2012 at 1:39pm
3- As globals: If you declare a global variable as static then it is encapsulated in that unit.
Being a global, all the functions defined in that cpp can see it, being static only those functions can see it.

With extern the variable is shared across the units. They are all the same variable.
Apr 3, 2012 at 3:33pm
extern means that the variable has its definition in another unit, so you must have exactly one that is not marked extern.
Apr 3, 2012 at 5:49pm
closed account (S6k9GNh0)
me555, not neccessarily just that .cpp, but that translation unit.
Last edited on Apr 3, 2012 at 9:54pm
Apr 3, 2012 at 9:54pm
closed account (S6k9GNh0)
Also, there are three types of linkage which pertains to the vision of a location. There's no linkage (stack variables, heap), internal linkage (where only that specific translation unit can see the location), and external linkage (where mostly anything can see the location). Global functions and variables are, by default, extern.

L B, I'm not catching on to what you mean?

@ OP, when making a library, it's good practice to declare internal functions "static" and exported functions "extern". However, in C++, you don't actually use "static" for this, you use an anonymous namespace like so:

1
2
3
namespace {
     void myFunc() { doStuff(); }
}
Last edited on Apr 3, 2012 at 9:54pm
Apr 4, 2012 at 12:39am
@computerquip: Consider that these are the only source files you have (and the only files involed at all):
1
2
3
4
5
//...

extern int MyVar;

//... 
1
2
3
4
5
//...

extern int MyVar;

//... 
1
2
3
4
5
//...

extern int MyVar;

//... 
All of these are declared external to the source files and thus the linker expect to find the definition elsewhere. Unfortunately, it can't - because it is never defined anywhere. Now if you did this:
1
2
3
4
5
//...

extern int MyVar;

//... 
1
2
3
4
5
//...

int MyVar;

//... 
1
2
3
4
5
//...

int MyVar;

//... 
The linker finds that there is more than one definition, and doesn't know which to use. The correct way to do it is for exactly one to not be marked extern:
1
2
3
4
5
//...

extern int MyVar;

//... 
1
2
3
4
5
//...

int MyVar;

//... 
1
2
3
4
5
//...

extern int MyVar;

//... 
Last edited on Apr 4, 2012 at 12:39am
Apr 5, 2012 at 11:07am
@LB That's exactly the problem that i have. I don't know if i have to make one definition putting "extern" or if it's better making your 3rd case. Making the main definition and putting extern property when i want to use the variable in other class.

Thanks you all. :)

Regards.
Apr 5, 2012 at 11:55am
You must have exactly one, no more and no less, definition that does not have 'extern'. So, use the third one. It is analogous with functions: prototypes are extern statements for functions, and the definition can only appear once out of everywhere.
Last edited on Apr 5, 2012 at 8:54pm
Apr 9, 2012 at 7:50am
Ok. Thank you very much :D
Topic archived. No new replies allowed.