Static usage and difference in C and C ++

Source: Internet
Author: User

Static has three main functions: (1) local static variables (2) external static variables/functions (3) both the first two types of static data member/member functions are available in C and C ++, and the third type is only available in C ++. The following describes respectively: 1. Local static variables are in C/C ++. Local variables can be divided into three types: auto, static, and register according to the storage format. Register is rarely used. The following describes the differences between auto and static. 1. Local variables of different auto types in bucket allocation and life cycle are common local variables (local variables without modification are of this type by default ). This type of local variable is stored on the stack. In the dynamic storage area, the lifecycle is limited to defining its functions. When the function ends, it is automatically released. Static local variables are stored in the static storage area and are not released during the entire running of the program. 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 will be 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 it is static or not.) features: static local variables "Memory" and survival "Global" II. External static variables/functions static have the second meaning in C: 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. 3. The static data member/member function (C ++ Special) C ++ reuses this keyword and gives it a third meaning different from the previous one: variables and functions that belong to a class rather than any specific objects of this class. this is the biggest difference between a function and a common member function and its application. For example, when counting an object of a class, counting the number of instances of the class is generated, you can use static data members. static does not limit the scope or extend the lifetime, but indicates the uniqueness of variables/functions in this class. this is also the meaning of "variables and functions that belong to a class rather than any specific objects of this class. because it is unique for the entire class, it cannot belong to an instance object. (For static data members, whether the member function is static or not, there is only one copy in the memory. When calling a common member function, this pointer must be passed in. When calling a static member function, no this pointer .)

Related Article

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.