Static global variables, static local variables, dynamic local variables

Source: Internet
Author: User

Static global variable: is the variable that can be used for methods in the current. M. Each time the call is invoked, the result of the last call is automatically used. Because only one memory address is allocated in the static allocation area, the numeric value is sliding scale.

Static local variables: the same as global variables, each time is automatically stored after the run value, the next time the runtime is directly used, but also only one address. is not released during the entire program run.

Dynamic local variables: often used, such as int a = 10, when a is a dynamic local variable. Each time the program is run to this place, a temporary allocation of an address, when the method is finished, the address is retracted, and the next time the method is called again, allocate a piece of memory.

Difference:

1. Assigning the initial value to a static local variable is done at compile time, that is, the initial value is assigned only once, and it has an initial value when the program is running. The initial value is not re-assigned each time the function is called, but at the end of the last function call.

Automatic variable assignment, not at compile time, but in the function call, each call function, re-give the initial value, equivalent to execute an assignment statement.

2. If the local variable is not assigned the initial value, for the static local variable, the compiler automatically assigns the initial value 0 (to the numeric type variable) or the null character (to the character type variable). In the case of an automatic variable, if the initial value is not assigned, it is an indeterminate value. This is because the storage unit has been disposed after each function call, and the next time it is called, the storage unit is reassigned, and the value of the allocated cell is indeterminate.

Static global variables, static local variables, dynamic local variables

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.