Lifetime and scope of static variables

Source: Internet
Author: User
Tags define local

The type description of static variables is static. Static variables are of course static storage, but the amount of static storage is not necessarily static variables. For example, although external variables are static storage, they are not necessarily static variables, A static external variable, or a static global variable, must be defined by static. For automatic variables, it is a dynamic storage method. However, static variables can also be defined as static automatic variables, or static local variables, to become a static storage method. From this point of view, a variable can be re-described by static and its original storage method can be changed.

1. Static local variables
Add the static identifier before the local variable description to form a static local variable. For example:

Static int A, B;
Static float array [5] = {1, 2, 3, 4, 5 };
Static local variables are stored in static mode and have the following features:
(1) Static local variables are defined in the function, but they do not exist when called as automatic variables, and disappear when you exit the function. Static local variables always exist, that is, their lifetime is the whole sourceProgram.
(2) Although the lifetime of the static local variable is the entire source program, its scope is still the same as that of the automatic variable, that is, the variable can only be used within the function that defines the variable. After exiting the function, although the variable still exists, it cannot be used.
(3) allow initial values to be assigned to the static local volume of the constructor class. If the initial value is not assigned, the system automatically assigns the value 0.
(4) If an initial value is not assigned to a static local variable of the basic type, the system automatically assigns the value 0. If the initial value is not assigned to the automatic variable, the value is not fixed. According to the characteristics of static local variables, it can be seen that it is a kind of lifetime for the entire source program. Although this function cannot be used after it is defined, it can continue to be used when the function is called again, and the value left after the previous call is saved. Therefore, when you call a function multiple times and require that the values of some variables be retained between calls, you can consider using static local variables. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is best to use local static variables.

2. Static global variables

The description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. When a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided. From the above analysis, we can see that after a local variable is changed to a static variable, its storage mode is changed, that is, its survival time is changed. After changing a global variable to a static variable, it changes its scope and limits its scope of use. Therefore, the description of static plays different roles in different places. Attention should be paid.

3. Static functions and static variables
In addition to the range, variables also have a retention period, during which variables can maintain their values. The value of module-level variables and public variables remains unchanged during the application's survival period. However, for dim declared local variables and declared local variables, these local variables only exist when the process is executed. Generally, when a process is completed, its local variable value does not exist and the memory occupied by the variable is also released. When this process is executed, all its local variables will be reinitialized.
However, you can define local variables as static variables to retain the value of the variables. In the process, use the static keyword to declare one or more variables, which is exactly the same as the dim statement:

Static depth
For example, the following function adds the previous total operating value stored in the static variable accumulate to a new value to calculate the total operating value.

Function runningtotal (Num)
Static applessold
Applessold = applessold + num
Runningtotal = applessold
End Function
If you use dim instead of static declaration of applessold, the previous cumulative value will not be retained by calling the function, and the function will simply return the same value that is called.
Declare applessold in the declaration section of the module and make it a module-level variable. This will also achieve the same effect. However, once this method changes the scope of the variable, the process no longer exclusively accesses the variable. Because other processes can also access and change variable values, the total operating value may be unreliable,CodeIt will be more difficult to maintain.

4. Declare all local variables as static variables
To make all local variables in the process static, you can add the static keyword at the beginning of the process header. For example:

Static function runningtotal (Num)
This changes all local variables in the process to static, Whether Declared in static, dim, or private or implicitly. Static can be placed before any sub or funtion process header, including the event process and the process whose name is private.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/slowgrace/archive/2008/11/23/3355097.aspx

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.