Volatile in C++
Understand volatile from assembly perspective.
1. Introduction
When we declare a variable as volatile
, it indicates that its value might be altered by external factors such as hardware, other threads, or signal handlers. A volatile variable cannot be optimized by the compiler, ensuring that the only way to obtain its value is by accessing its memory address directly.
2. Examples
Let’s try a simple example first and optimize it with -O3
option.
C++
1 | int main() { |
Assembly
1 | main: |
Since -O3
perform aggresive optimization and the compiler recognizes that age
is only assigned and reassigned within the scope of main, and its value is never used afterward. Therefore, both the initial and subsequent assignments to age are redundant. The optimized code will eliminate the age variable entirely.
But when we declare age as volatile
.
C++
1 | int main() { |
Assembly
1 | main: |
Any modification to a volatile variable directly accesses its memory address. The compiler does not optimize volatile variables.
Volatile in C++