Auto, static, register, const, volatile, and extern

Source: Internet
Author: User

From: http://blog.sina.com.cn/s/blog_4b9216f50100ean1.html

Auto

This keyword is used to declare that the lifetime of a variable is automatic, and variables not defined in any class, structure, enumeration, union, or function are considered global variables, the variables defined in the function are considered as local variables. This keyword is rarely written, because all variables are auto by default.

Register

This keyword command is used by the compiler to store variables in the internal registers of the CPU as much as possible, instead of accessing the variables through memory addressing to improve efficiency.

Static

Two common uses:
1> count the number of times a function is called;
2> reduce the overhead of local array creation and assignment. Variable creation and assignment require a certain amount of processor overhead, especially Storage types containing many elements, such as arrays. In some functions that contain a large amount of changes and are often called, you can declare some arrays as static types to reduce the overhead of creating or initializing these variables.

Detailed description:
1> variables will be placed in the global storage area of the program, so that the original values can be maintained during the next call. This is the difference between stack variables and heap variables.
2> the variable uses static to notify the compiler that it is only visible within the scope of the variable. This is the difference between it and global variables.
3> when static is used to modify a global variable, it changes the scope of the global variable so that it cannot be extern by other programs and is restricted to the current file, however, the storage location remains unchanged in the Global static storage zone.

Note:
1> If the global variable is only accessed in a single c file, you can change the variable to a static global variable to reduce the coupling between modules;
2> If the global variable is only accessed by a single function, you can change the variable to the static local variable of the function to reduce the coupling between modules;
3> when designing and using functions that access dynamic global variables, static global variables, and static local variables, you need to consider the re-import problem (the same output should be generated if the input data is the same ).

Const

All things modified by const are protected by force, which can prevent unexpected changes and improve program robustness. It can modify the parameters, return values, and even the definition body of a function.

Purpose:
1> modify input parameters
A. For non-Internal data type input parameters, the "value transfer" method should be changed to "const reference transfer" to improve efficiency. For example, change void func (A) to void func (const A & ).
B. For internal data type input parameters, do not change the "value transfer" method to "const reference transfer ". Otherwise, it will not achieve the goal of improving efficiency, but also reduce the comprehensibility of functions. For example, void func (int x) should not be changed to void func (const Int & X ).
2> Use const to modify the return value of a function
A. if the const modifier is added to the return value of a function in the "pointer passing" mode, the content of the function return value (that is, the pointer) cannot be modified. The return value can only be assigned to the same type pointer with the const modifier.
For example, const char * getstring (void );
The following statement causes a compilation error:
Char * STR = getstring (); // cannot convert from 'const char * 'to 'Char *';
The correct usage is:
Const char * STR = getstring ();
B. If the return value of a function adopts the "value transfer mode", since the function copies the return value to an external temporary storage unit, adding the const modifier has no value. For example, do not write the int getint (void) function as const int getint (void ).
3> In the const member function declaration, the const keyword can only be placed at the end of the function declaration, indicating that the class member does not modify the object.

Note:
Const type M; // modify m to unchangeable
Example:
Typedef char * pstr; // New Type pstr;
Char string [4] = "ABC ";
Const char * P1 = string;
P1 ++; // correct. The above modifier is * P1, and P1 is variable.
Const pstr P2 = string;
P2 ++; // error. The above modifier is P2, P2 is unchangeable, and * P2 is variable.
Similarly, when the const modifies the pointer, this principle will not be confused.
Const int * value; // * The value is unchangeable and the value is variable.
Int * const value; // The value is unchangeable. * The value is variable.
Const (int *) value; // (int *) is a type, value is unchangeable, * value is variable
// Logically, the compilation fails. tydef int * newtype is required;
Const int * const value; // * value and value cannot be changed.

Volatile

It indicates that the value of a variable may be changed externally. When using this variable, the optimizer must carefully read the value of this variable every time instead of using the backup stored in the register. It can be applicable to basic types such as int, Char, long... and C structures and C ++ classes. When volatile is used to modify a structure or Class Object, all
All members are considered as volatile.
This keyword is often used in a multi-threaded environment, because the same variable may be modified by multiple threads when a multi-threaded program is compiled, and the program synchronizes various threads through this variable.
Simple Example:
DWORD _ stdcall threadfunc (lpvoid signal)
{
Int * intsignal = reinterpret_cast (signal );
* Intsignal = 2;
While (* intsignal! = 1)
Sleep (1000 );
Return 0;
}
Set intsignal to 2 when the thread starts, and then wait until it exits when intsignal is 1. Obviously, the intsignal value must be changed externally; otherwise, the thread will not exit. However, the thread does not exit during actual operation. Even if the value of the thread is changed to 1 in the external department, you can see the corresponding pseudo assembly code:
MoV ax, signal
Label:
If (ax! = 1)
Goto label
For the C compiler, it does not know that this value will be modified by other threads. Naturally, it is cached in the register. The C compiler does not have the thread concept. Volatile is used in this case. Volatile indicates that the value may be changed outside the current thread. That is to say, we need to add the volatile keyword before intsignal in threadfunc.
The compiler knows that the value of the variable will change externally. Therefore, each time the variable is accessed, it will read the variable again and the loop will change to the following pseudo code:
Label:
MoV ax, signal
If (ax! = 1)
Goto label

Note: A parameter can be either const or volatile, because it may be unexpectedly changed. It is const because the program should not try to modify it.

Extern

Extern indicates "external". · its function is to tell the compiler that this variable may not exist in the current file, but it must exist in a source file in the project or the output of a DLL.

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.