What does the volatile qualifier actually guarantee for an object in a C program?
Correct answer
D. Every access written in the abstract machine is performed, so the compiler cannot cache it
16 views
D. Every access written in the abstract machine is performed, so the compiler cannot cache it
16 views
volatile tells the compiler the object may change outside the program's control, so each access must be emitted rather than cached in a register or coalesced with another. It gives no atomicity, no ordering against other threads and no memory barrier; those require std::atomic or explicit fences. Confusing volatile with thread synchronisation is a standard trap, especially for memory-mapped registers.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.