The C compiler will optimize variables and code, and let's take a look at the following example.
int a,b,c;
A=1;
B=a;
C=b;
This program normally works like this: First put a point in the memory space into 1
And then read the number in the memory space of a pointing to the memory space where B points,
Finally, the number in the memory space where B points is read out and placed in the memory space C points to.
However, the compiler will be a certain optimization of this program, compile time, directly put 1 into three memory space.
This is a good thing in normal operation, but there will always be some special situation, for example, an interrupt program suddenly changed the value of a, then this happens: B and C should be a change after the value, but the compiler after the optimization has become a a,b,c is 1, which makes people depressed.
The workaround, then, is to add a volatile keyword to the definition of the variable, such as:
volatile int A;
This way the compiler will not be able to give you any optimizations when it encounters an operation of a, which ensures that your program works as you expect, but that if you use volatile in large quantities, without optimization, you can reduce the efficiency of your program.
Originally C language running efficiency is much lower than the assembly, so that volatile in addition to try to consider as well as possible, because many programs run in particular focus on efficiency.
The second optimization is the Restrict keyword, which is used only to modify pointers, and it is not useful to modify different variables.
When you use a restrict-modified pointer that is not changed by something else, to improve the efficiency of execution, you will be modified at compile time.
For example, the following function:
int main (void)
{
int *restrict P1, *restrict P2, A;
*p1=3;
*p2=5;
a=2+ (*P2);
return 0;
}
After this, we did the optimization, the compiler at the time of compilation will directly convert the *P2 to 5 (because there is no instruction to modify it, so the direct use of 5来 instead of *P2)
There are pros and cons to optimization, so use it carefully anyway.
compiler optimization of C language