|
|
|
|
|
by empiricus
2585 days ago
|
|
This is my understanding of volatile as well: volatile just forces read/write to memory. What a read/write to memory entails is a different story. What happens with no volatile is again another story.
If my understanding is wrong, someone please enlighten me. |
|
For a trivial example, see this code:
As you can see from [1], a smart compiler will calculate the sum at compile time and make the function simply return the resulting number (i.e., no loop is generated).If you make "sum" volatile, the compiler is forced to do the loop[2].
[1] https://godbolt.org/z/3sX5mU
[2] https://godbolt.org/z/F5CiDJ