c ++ learning - variable

The difference between C ++ global variables, local variables, static global variables, static local variable

C ++ variable depending on the position of the life cycle defined, with different scopes, scopes can be divided into 6 kinds: global scope , local scope , the statement of scope , class scope , namespace scope File scope and .

From the look Scope:

Global variables have a global scope. Simply define global variables in a source file, you can act on all the source files. Of course, other source file does not contain definitions of global variables need to use the extern keyword to declare the global variable again.

Static local variable with local scope, it is only initialized once, the first time since the initialization program runs until the end of all there has been, it is a global variable and the global variable difference is that all functions are visible, but static local variable only to define your own functions is always visible.

Local variables only local scope, it is automatically an object (auto), it is not always present during the program is running, but exist only during the execution of the function, a function is called after the execution, variables are revoked, they occupied memory also be withdrawn.

Static global variables have global scope, it is the difference between a global variable is that if the program contains multiple files, which define its role in the file, can not act to another file, namely the static keyword has modified variables file scope. So even if two different source files have the same name defined static global variables, they are also different variables.

From the allocated memory space to see:

Global variables, static local variables, static global variables are assigned space in the static storage area, and local variables allocate space on the stack.

Global variables itself is static storage mode, static global variables of course, a static storage. This is no different in the two storage methods. Although this difference is non-static global variable scope is the whole source, when a source program by a plurality of source files, non-static global variable in each source file is valid. A static global variables are restricted in its scope, that is only valid within the definition of the variable in the source file, it can not use the same files in the other sources in the source program. Due to the limited scope static global variables within a source file, can only function in the source file for the public, so to avoid causing errors in the other source files.

  •  1), static variables will be placed static data storage area program (data segment) (globally visible), so the next time you can call also can keep the original assignment. This is the difference between it and stack variables and stack variables.
  •  2), to inform the compiler static variables, their only visible inside the scope of variables. This is the difference between it and global variables.

From the above analysis, the local variables as static variables after the change is to change its storage that is changing its survival. After changing global variables as static variables are changed its scope, limiting its scope. Thus static specifier in different places role is different. It should be noted.

Tips:

  • A, only if the global variables accessed in a single file C, this variable may be modified as a static global variable, in order to reduce the coupling between modules;
  • B, when only a single global variable access function, this variable may be changed to a static local variable of the function, to reduce the coupling between modules;
  • C, design and use of dynamic access global variables, static global variables, static local variables of the function, we need to consider the re-entry problem, because they are on static data storage area, global visibility;
  • D, if we need a reentrant function, then we must avoid static variables used in the function (this function is called: function with "internal memory" functions)
  • E, the function must be used in the case of static variables: for example, when a function returns a value of type pointer, it must be the address of the static local variable as a return value, if the type of auto, then the pointer returns error.

------------------------------------------------------------------

static global variables : change the scope without changing the storage location

static local variables : change the storage location, without changing the scope

Static function  : Before the return type of the function together with the static keyword, the function has been defined as static functions. Different static function and normal function, it can only declare its file which can be seen, it can not be used by other files.

If the definition of a function in a source file, the function can only be called in this document, the function call can not be the same program in other files, this function is also referred to as an internal function. Define an internal function, just before the function type plus a "static" keywords.

Guess you like

Origin www.cnblogs.com/chrisgo/p/11648336.html