Static usage in C Language

Source: Internet
Author: User

The static keyword is a keyword that exists in both C and C ++. It mainly has three usage methods. The first two are only used in C, and the third is used in C ++. This article only describes how to use
(1) local static variables
(2) external static variables/functions
(3) static data member/member functions

I. Local static variables
In C/C ++, local variables can be divided into three types: auto, static, and register.

Compared with the auto-type (common) local variable, the static local variable has three differences.
1. Different Storage space allocations
The auto type is allocated to the stack and belongs to the dynamic storage class, occupying the dynamic storage space. The function is automatically released after the function call is completed, and the static type is allocated to the static storage area, the program is not released during the entire running period. they have the same scope but different lifetime.
2. Static local variables initialize at the first run of the module and operate only once
3. for local static variables, if no initial value is assigned, the initial value 0 or null characters are automatically assigned during the compilation period, while the initial values of the auto type are uncertain. (except for class objects in C ++, if the class object instance is not initialized, the default constructor is automatically called, regardless of whether the class is static or not)

Features: "Memory" of static local variables and "Global" of lifetime"
The so-called "Memory" refers to the value of the first function call exit when the second function call enters.

Note:
1. "Memory", a very important part of the program running is repeatability, while the "Memory" of static variables damages this repeatability, resulting in different runtime results may be different.
2. global and uniqueness of "Lifetime. the storage space of common local variables is allocated to the stack. Therefore, the allocated space may be different each time a function is called. Static has the global uniqueness feature, all point to the same memory, which leads to a very important problem-re-import is not allowed !!!
In this way, pay special attention to this issue in multi-threaded or recursive programming.

Ii. External static variables/functions
Static in C has a second meaning: it is used to indicate global variables and functions that cannot be accessed by other files ., However, to restrict the scope of global variables/functions, add static before a function or variable to make the function a static function. However, the meaning of "static" here is not the storage method, but the scope of the function is limited to this file (so it is also called internal function ). Note that, whether static or not, external (global) variables are stored in the static storage area and the lifetime is global. at this time, static only applies to the scope, and the scope is limited within this module (file.
The advantage of using internal functions is that when different people write different functions, you don't have to worry about your own defined functions and whether they will have the same name as the functions in other files.

Third, applications in C ++

 

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.