Static variable life cycle. -=---

Source: Internet
Author: User
Tags valid
Static life cycle 2011-07-15 16:01

The type descriptor for a static variable is static. Static variables, of course, are static storage, but the amount of static storage is not necessarily static variables, such as external variables are static storage, but not necessarily static variables, must be defined by static to be static external variables, or static global variables. For automatic variables, it belongs to the dynamic storage mode. But you can also use static to define it as a static automatic variable, or a static local variable, to become a static storage mode. As a result, a variable can be re-described by static and change its original storage mode.

1. Static local Variables
A static local variable is formed by adding the static specifier before the description of the local variable.  For example: Static int A, B; static float array[5]={1,2,3,4,5};

Static local variables belong to static storage, which has the following characteristics:
(1) A static local variable is defined within a function, but not as an automatic variable, when it is called and disappears when the function is exited. A static local variable always exists, that is, its lifetime is the entire source program.
(2) The lifetime of a static local variable is the entire source program, but its scope is still the same as the automatic variable, that is, the variable can only be used within the function that defines it . After exiting the function, it cannot be used even though the variable continues to exist.
(3) allows the initial value to be assigned to the static local quantity of the construction class. If the initial value is not assigned, the system automatically assigns 0 values.
(4) If the static local variable of the basic type is not assigned the initial value in the description, the system automatically assigns 0 values. The value of the automatic variable is indeterminate if it is not assigned the initial values. According to the characteristics of static local variables, it can be seen that it is a lifetime of the whole source of the program amount. Although it cannot be used after the function that defines it, it can continue to be used if the function that defines it is called again, and the value left after the previous call is saved. Therefore, you can consider static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although this can be achieved with global variables, global variables sometimes cause unintended side effects, so it is advisable to use local static variables.

2. Static Global Variables

The description of the

global variable (external variable) is static as a global variable, before it is prefixed with static. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can only be common to functions within that source file , so you avoid causing errors in other source files. From the above analysis, it can be seen that the change its storage mode after changing the local variable to a static variable, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use . So the function of the static descriptor is different in different places. Should be taken into consideration.

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.