Auto in C language the register the static const and volatile difference parsing

  • 2020-04-02 01:35:04
  • OfStack

1) auto
This keyword is used to declare that the lifetime of a variable is automatic, and that variables that are not defined in any class, structure, enumeration, union, and function are treated as global variables, while variables defined in a function are treated as local variables. This keyword is not used much because all variables are auto by default.

(2) register
This keyword commands the compiler to store variables in internal CPU registers as much as possible instead of accessing them through memory addressing for efficiency.

(3) the static
Two common USES:
1 > Count the number of times the function is called;

2 > Reduce the overhead of local array creation and assignment. Variable creation and assignment require some processor overhead, especially for storage types with more elements such as arrays. In functions that have more variables and are called more often, you can declare arrays as static types to reduce the overhead of creating or initializing those variables.

Details:
1 > , variables are placed in the program's global store so that they can be assigned the same value on the next call. This differentiates it from stack and heap variables.

2 > , variable static tells the compiler that it is only visible within the scope of the variable. This is what distinguishes it from 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 used by other extern programs, limiting it to the current file, but not changing its location.

Usage note:
1 > If the global variable is only accessed in a single C file, it can be changed to a static global variable to reduce the coupling between modules.

2 > If the global variable is only accessed by a single function, the variable can be changed 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 reentrant problem (the same output should be produced as long as the input data is the same).

(4) const
Everything modified by const is protected against unexpected changes and improves program robustness. It can modify a function's arguments, return values, and even its definition body.

Function:
1 > Modify input parameter
A. For input parameters of non-internal data types, the "value pass" method should be changed to "const reference pass" to improve efficiency. For example, replace void Func(A A) with void Func(const A & A).

B. For input parameters of internal data types, do not change the method of "value passing" to "const reference passing". Otherwise, it can not achieve the purpose of improving efficiency and reduce the comprehensibility of the function. For example, void Func(int x) should not be changed to void Func(const int &x).

2 > Modifies the return value of the function with const
A. If the function return value in "pointer pass" mode is modified with const, the contents of the function return value (pointer) cannot be modified, and the return value can only be assigned to the same type of pointer with const modification.
Const char * GetString(void);

A compilation error will appear in the following statement:
Char * STR = get string (); //cannot convert from 'const char *' to 'char *';

The correct usage is:
Const char * STR = GetString();

B. If the return value of a function is "passed in value", there is no value in adding const since the function copies the return value to an external temporary storage location. For example, do not write the function int GetInt(void) as const int GetInt(void).
3 > In the declaration of a const member function, the const keyword can only be placed at the end of the function declaration to indicate that the class member does not modify the object.

Description:
Const type m; // modifies m to be immutable
Example:
Typedef char * pStr; // new type pStr;
Char string [4] = "ABC";
Const char *p1 = string;
P1 + +; // true, this is *p1,p1 is variable
Const pStr p2 = string;
The p2 + +; // error, above modification is p2, p2 immutable,*p2 variable

In the same way, the const modifies Pointers with this principle without confusion.
Const int * value; //*value is immutable, value is variable
Int * const value; //value is immutable, *value is mutable
Const value (int *); //(int *) is a type,value immutable,*value variable
// logically, the compilation cannot pass, tydef int* NewType is required;
Const int * const value; //*value,value is immutable

(5) the volatile
Indicates that the value of a variable may be changed externally, and the optimizer must carefully reread the value of that variable each time it is used, rather than using a backup stored in a register. It can be applied to base types such as int,char,long... It also applies to C structures and C++ classes. When a structure or class object is treated with volatile, all members of the structure or class are treated as volatile.
This keyword is often used in multithreaded environments because when writing multithreaded programs, the same variable may be modified by multiple threads, and the program synchronizes the threads through this variable.

Simple example:


 DWORD __stdcall threadFunc(LPVOID signal)
 {
 int* intSignal="reinterdivt"_cast(signal);
 *intSignal=2;
 while(*intSignal!=1)
 sleep(1000);
 return 0;
 }

The thread starts with intSignal set to 2 and then loops until it exits when intSignal is 1. Obviously, the value of intSignal must be changed externally or the thread will not exit. However, when the thread is actually running, it will not exit, even if the value of it is changed to 1 in the external, take a look at the corresponding pseudo-assembly code to see:

 mov ax,signal
 label:
 if(ax!=1)
 goto label

For the C compiler, it does not know that the value will be modified by another thread. Naturally, it is cached in a register. The C compiler has no concept of threading, so volatile is needed. Volatile means that the value may change outside the current thread. That is, we are going to prefix the intSignal in threadFunc with the volatile keyword. At this point, the compiler knows that the value of the variable will change externally, so it reads it again each time it accesses the variable, and loops as shown in the following pseudo-code:

 label:
 mov ax,signal
 if(ax!=1)
 goto label 

Note: A parameter can be either const or volatile, volatile because it can change unexpectedly. It's const because the program shouldn't try to change it.

(6) extern
Extern is used to tell the compiler that with this variable, it may not exist in the current file, but it must exist in a source file in the project or in the output of a Dll.


Related articles: