C language understanding

"Volatile", in C language, this keyword is used to indicate that the variable is volatile, requiring the compiler not to optimize. The members in these structures all represent registers, and the registers are often modified by the peripheral or STM32 chip state, which means that even if the CPU does not execute the code to modify these variables, the value of the variable may be modified and updated by the peripheral. So every time these variables are used, we ask the CPU to revisit the address of the variable. Without this keyword modification, in some cases, the compiler thinks that there is no code to modify the variable, and directly obtains the value of the variable from a CPU cache, which can speed up the execution speed, but the cache is stale data , The latest status of the register we require may be different.

Guess you like

Origin www.cnblogs.com/action0/p/12757836.html