Problem:
const volatile int i=10; Is there a problem with this line of code? If not, what is the nature of that I?
Reply:
No problems, such as read-only status registers. It is volatile because it can be changed unexpectedly; it is const because the program should not attempt to modify it.
Volatile and const are not contradictory, but the scope of control is not the same, one is outside the program itself, the other is the program itself.
Const represents (runtime) constant semantics: const-decorated objects cannot be modified at their scope, and the compiler generates compilation errors for expressions that attempt to directly modify the const object.
Volatile means "volatile", that is, the runtime object may be modified outside the control flow of the current program context (for example, multiple threads are modified by other threads; the memory where the object resides may be randomly modified by multiple hardware devices, etc.)
: A volatile object that the compiler does not optimize for the operation of this object. An object can be modified at the same time by both const and volatile, indicating that the object embodies constant semantics, but it may be modified by an unexpected situation in the context of the program in which the current object resides.
const volatile INT i