Last updated:2026-10-12
C++ volatile Keyword¶
About the Author:¶
Personal WeChat official account:

1. volatile¶
Reading or writing a variable qualified with volatile triggers some observable side effects, and those observable side effects are determined by factors outside the program.
2. Applications of volatile¶
(1) Hardware registers of parallel devices (such as status registers). Suppose we want to initialize a device, and one of its registers is 0xff800000.
int *output = (unsigned int *)0xff800000; //定义一个IO端口;
int init(void)
{
int i;
for(i=0;i< 10;i++)
{
*output = i;
}
}
static int i=0;
int main()
{
while(1)
{
if(i) dosomething();
}
}
/* Interrupt service routine */
void IRS()
{
i=1;
}
volatile bool bStop=false; //bStop 为共享全局变量
//第一个线程
void threadFunc1()
{
...
while(!bStop){...}
}
//第二个线程终止上面的线程循环
void threadFunc2()
{
...
bStop = true;
}
Knowing the application scenarios of volatile is an effective way to distinguish C/C++ programmers from embedded developers: embedded folks constantly deal with hardware, interrupts, RTOSes and so on, all of which require volatile variables. Not understanding volatile invites disaster in program design.
3. Common volatile Questions¶
The questions below show whether an interviewee truly understands volatile. (1) Can a parameter be both const and volatile? Why? Yes. One example is a read-only status register. It is volatile because it may be changed unexpectedly. It is const because the program should not try to modify it. (2) Can a pointer be volatile? Why? Yes, although this is not common. One example is when an interrupt service routine modifies a pointer that points to a buffer. (3) What is wrong with the following function?
This code is a bit devious. Its intent is to return the square of the value pointed to by ptr, but since ptr points to a volatile parameter, the compiler will produce something like the following: Since the value of *ptr may change unexpectedly, a and b may be different. As a result, this code may not return the square you expect! The correct code is as follows:4. Using volatile¶
-
The volatile keyword is a type modifier; a variable declared with it can be changed by factors unknown to the compiler (the operating system, hardware, other threads, etc.). Use volatile to tell the compiler not to optimize such objects.
-
A variable declared volatile must be read from memory on every access (a variable not qualified with volatile may be read from a CPU register because of compiler optimization)
- const can be volatile (e.g. a read-only status register)
- Pointers can be volatile
Code to study:
Related Topics¶
- C++ Pure Virtual Functions and Abstract Classes | C++那些事
- C++ assert | C++那些事
- C++ Bit Fields | C++那些事
- Implementing C++ Polymorphism in C | C++那些事
- C++ const Constants and Qualifiers | C++那些事
- The Complete C++ const Family | C++那些事
- C++ Dereference Operator * | C++那些事
- C++ Initialization and Assignment | C++那些事
- Ways to Return Multiple Values in C++ | C++那些事
- C++ mutable Keyword | C++那些事