Register
Variables declared using the modifier register belong to the Register storage type. This type is similar to the automatic storage type, with automatic storage period,CodeBlock scope and inner connection. Declared as register is only a request, so this variable may still be a common automatic variable. In either case, the address cannot be obtained for the variable modified with register. If it is not initialized, its value is not fixed.
Volatile
Volatile tells the CompilerProgramIt may be modified by other proxies or threads. Therefore, when using the value of a variable declared by volatile, the system always reads data from its memory instead of using the cached value in the register. For example,
Val1 = X;
Val2 = X;
If volatile is not declared, the system may directly read X from the register rather than from the initial location of the memory when assigning values to val2. Between two values, X may be completely changed by some unknown factors of the compiler (such as the operating system, hardware, or other threads ). If declared as volatile, the compiler will not use the cache, but re-read X from the memory each time.
Restrict
Restrict is introduced by c99. It can only be used to restrict pointers and indicates that pointers are the only and initial method for accessing a data object. Consider the following example:
Int ar [10]; int * restrict restar = (int *) malloc (10 * sizeof (INT); int * par = Ar;
The restar is the only and initial method to access the memory allocated by malloc. No. So:
For (n = 0; n <10; n ++) {Par [N] + = 5; restar [N] + = 5; Ar [N] * = 2; par [N] + = 3; restar [N] + = 3 ;}
Because restar is the only and initial way to access allocated memory, the compiler can optimize the restar operation: restar [N] + = 8 ;. The PAR is not the only way to access the array AR, so the following optimization cannot be performed: par [N] + = 8 ;. Because before par [N] + = 3, ar [N] * = 2 is changed. With the keyword restric, the compiler can be optimized with confidence. This keyword is said to have come from the ancient FORTRAN.
Summary
Two keywords: volatile and restrict are used to facilitate Compiler optimization.