Static and external variables

What is the difference between static and external variables? What are the benefits of creating a static variable? Why do we prefer to make external variables in multifunctional programs? The book says that it is still very late to initialize. I do not understand.

+4
source share
2 answers

The problem is that in static C they can have different meanings. I will try to give an overview of the various situations in the following paragraphs.

If a variable is defined outside the function, it can be used by all functions in the file. Sometimes also called a "global variable." This means that there is only one instance of this variable for the entire file. The variable name is also stored in the resulting .OBJ file. The latter is important because, if another file also defines a variable with the same name outside the function, the linker assumes that in both cases it has the same variable and combines them. To make this clearer, it is best to add the keyword "extern" to one of the variables. In this case, we say that we declare a variable, and not define it. This is an additional signal to the compiler / linker, indicating that we really want to refer to a global variable defined elsewhere.

If you want to define a global variable, but do not want it to be accessible to other files, add the static keyword earlier. The static keyword tells the compiler that the variable name should not be stored in the .OBJ file. This means that there are two .C files with the following line:

static long MyGlobalVariable; 

will have their own variable. Both variables will be called MyGlobalVariable.

If you define a variable inside a function, it becomes a local variable. It occurs if the function is called and disappears again after the function finishes. In some situations, you want to store the value of a variable between function calls. You can do this using a global variable (instead of a local variable), but then this variable becomes available for all functions in the file that you do not need. In this case, you can put the static keyword before the variable, for example:

 void MyFunction() { static long MyLocalVariable = 0; ++MyLocalVariable; } 

The first time the MyLocalVariable function is called, it will be "created" and initialized with the value 0. At the end of the function, the variable will not be destroyed, but it will be saved. Therefore, the next time this function is called, the value of the variable will be 1, not zero.

In C, it really doesn't matter if you put the variable outside the function (like a global variable) or define it as static inside the function. The only difference is where you can access the variable.

In C ++, everything is completely different. If you write this (out of function):

 MyClass MyGlobalVariable; 

MyGlobalVariable will be constructed (this will be: the constructor will be executed) at the beginning of the application, before the main one is called. However, you do not have real control over the order in which all global variables are built. Therefore, if another file contains the following:

 MyOtherClass MySecondGlobalVariable; 

You cannot know for sure whether MyGlobalVariable or MySecondGlobalVariable will be created first. This can cause problems if the constructor of one of them relies on the presence (construction) of the other.

On the other hand, if you define a variable as static inside a function:

 void MyFunction() { static MyClass MyStaticVariable; } 

Then MyStaticVariable will be constructed the first time the function is called. Using this construct, you can write something like this:

 MyClass &getMyClass() { static MyClass MySelf; return MySelf; } 

And we implemented a singleton where we control it when it is built. The first time we needed it, it was built.

Honestly, this approach is quite simplistic, as it can lead to problems in multi-threaded applications. In this case, there are other tricks.

+24
source

"Static" more or less means "it always will be." Depending on the context, you get different results:

  static int global_variable; void function() { static int global_function_variable; } class foo { static void function() { static int foo_function_variable; //... } static int foo_variable; } 

global_variable - Only ever visible inside a "translation unit". (Since the headers are more or less copied, does this mean that the static global in the header exists as separate variables for all the cpp files that it belongs to?)

global_function_variable is created on the first call to function and will exist throughout the entire program life cycle. The value is saved, so if you change it in one function call, the next function call will use the changed variable.

foo::function() is a function that can be called a global function; you do not need to have an instance of foo to call it. This means that it does not have a valid this pointer. Similarly, foo_variable exists even if there are no foo objects. All foo objects have the same foo_variable - if you change it in one object, all other objects “see” the change.

foo_function_variable behaves, as it seems to me, as global_function_variable .

The basic idea of extern is to manage the variable binding: From MSDN :

 // specifying_linkage1.cpp int i = 1; void other(); int main() { // Reference to i, defined above: extern int i; } void other() { // Address of global i assigned to pointer variable: static int *external_i = &i; // i will be redefined; global i no longer visible: // int i = 16; } extern int global_variable; 

extern can also be used to bind a variable using another language, most often C.

global_variable - the most common use of extern; he says that in another place there is a variable called global_variable, and we will use this variable. Somewhere you need to declare a variable without the extern keyword, or it is never created.

-3
source

Source: https://habr.com/ru/post/1314483/


All Articles