Static variable problems

Source: Internet
Author: User

Static:

I. Control Storage Methods:

Static is introduced to inform the compiler that the variables are stored inProgramInstead of stack space.

1. Cause: when the program executes the variable defined in the function to its definition, the compiler allocates space for it on the stack, the space allocated by the function on the stack is released at the end of the function execution. This creates a problem: If you want to save the value of this variable in the function to the next call, how to implement it?

The easiest way to think of is to define a global variable, but defining a global variable has many disadvantages, the most obvious drawback is that the access range of the variable is broken (so that the variables defined in this function are not controlled by this function ).

2. Solution: Therefore, static is introduced in C ++ to modify the variable, which can instruct the compiler to save the variable in the static storage area of the program, this achieves the goal and keeps the access range of this variable unchanged.

Ii. control visibility and connection type:

Static also has a function, which limits the visible scope of the variable to the compilation unit and makes it an internal connection. In this case, its antonym is "extern ".

Static analysis summary: static always changes the storage form of variables or objects to static storage, and the connection mode to internal connections. For local variables (internal connections already exist ), it only changes the storage mode. For global variables (which are already in static storage), it only changes the connection type.

Static member in the class:

I. Causes and functions:

1. Interaction between objects in a class is required, that is, a data object must serve the entire class rather than a certain object.

2. At the same time, we strive not to undermine the encapsulation of the class, that is, this member is required to be hidden inside the class and invisible to the outside world.

The static member of the class meets the preceding requirements because it has the following features: it has an independent storage zone and belongs to the entire class.

2. Note:

1. for static data members, the connector ensures that it has a single external definition. Static data members are initialized sequentially according to the sequence in which they appear. Note that when static members are nested, make sure that the nested members have already been initialized. The order of cancellation is the reverse order of initialization.

2. The static member function of the class belongs to the entire class rather than the class object, so it does not have the this pointer, which leads to the ability to only compile static data and static member functions of the class.

Static is a common modifier in C ++. It is used to control the storage and visibility of variables. Next I will talk about the reason and role of static modifiers, the essence of the static modifier is comprehensively analyzed.

1. Static global variables
Add the keyword static before the global variable, which is defined as a static global variable. Here is an example of a static global variable:
// Example 1
# Include <iostream. h>
Void FN ();
Static int N; // defines the static global variable.
Void main ()
{N = 20;
Cout <n <Endl;
FN ();
}
Void FN ()
{N ++;
Cout <n <Endl;
}
Static global variables have the following features:
This variable allocates memory in the global data zone;
Uninitialized static global variables will be automatically initialized to 0 by the program (the values of automatic variables are random unless they are explicitly initialized );
Static global variables are visible in the entire file declared, but invisible outside the file;
All static variables are allocated memory in the global data zone, including the static local variables to be mentioned later. For a complete program, the distribution in the memory is as follows:
Code Zone
Global data Zone
Heap Area
Stack Zone
In general, dynamic data generated by new is stored in the heap zone, and automatic variables in the function are stored in the stack zone. Automatic variables usually release space as the function exits, and static data (even static local variables in the function) is stored in the global data zone. The data in the global data zone does not release space because the function exits. Careful readers may find that
Static int N; // defines the static global variable.
Change
Int N; // defines the global variable
The program runs normally.
Indeed, defining global variables can share variables in files, but defining static global variables has the following benefits:
Static global variables cannot be used by other files;
Variables with the same name can be defined in other files without conflict;
You can change the Sample Code as follows:
// Example 2 // file1
# Include <iostream. h>
Void FN ();
Static int N; // defines the static global variable.
Void main ()
{N = 20;
Cout <n <Endl;
FN ();
}
// File2
# Include <iostream. h>
Extern int N;
Void FN ()
{N ++;
Cout <n <Endl;
}
Compile and run example 2, and you will find that the above Code can be compiled separately, but an error occurs during running. Try
Static int N; // defines the static global variable.
Change
Int N; // defines the global variable
Compile and run the program again to understand the differences between global variables and static global variables.
2. Static local variables
Before a local variable, add the keyword static to define the variable as a static local variable.
Here is an example of static local variables:
// Example 3
# Include <iostream. h>
Void FN ();
Void main ()
{Fn ();
FN ();
FN ();
}
Void FN ()
{Static n = 10;
Cout <n <Endl;
N ++;
}
Generally, a variable is defined in the function body, and stack memory is allocated to the local variable whenever the program runs the statement. However, as the program exits from the function body, the system will reclaim the stack memory and the local variables will also become invalid.
But sometimes we need to save the variable value between two calls. The general idea is to define a global variable for implementation. In this way, the variable no longer belongs to the function itself, and is no longer only controlled by the function, causing inconvenience to program maintenance.
Static local variables can solve this problem. Static local variables are stored in the global data zone, instead of in the stack. Each value is kept to the next call until the next value is assigned.
Static local variables have the following features:
This variable allocates memory in the global data zone;
Static local variables are initialized for the first time when the program executes the declaration of this object, that is, function calls will not be initialized in the future;
Static local variables are generally initialized at the Declaration. If no Explicit initialization is performed, the static local variables will be automatically initialized to 0 by the program;
It always resides in the global data zone until the program running ends. However, its scope is local scope. When the function or statement block that defines it ends, its scope ends;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.