What does the volatile keyword mean in C++?

C++Server Side ProgrammingProgramming

volatile means two things −

- The value of the variable may change without any code of yours changing it. Therefore whenever the compiler reads the value of the variable, it may not assume that it is the same as the last time it was read, or that it is the same as the last value stored, but it must be read again.

- The act of storing a value to a volatile variable is a "side effect" which can be observed from the outside, so the compiler is not allowed to remove the act of storing a value; for example, if two values are stored in a row, then the compiler must actually store the value twice.

As an example −

i = 2;
i = i;

The compiler must store the number two, read the variable i, store the variable that it read into i.
You can find more details on the volatile keyword here − https://www.geeksforgeeks.org/understanding-volatile-qualifier-in-c/

raja
Published on 15-Feb-2018 12:17:15
Advertisements