
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is volatile keyword in C++?
C++ Volatile Keyword
In C++, the volatile keyword is a type qualifier that tells the compiler that the value of a variable may be changed at any time unexpectedly, so it should not optimize access to that variable. This change in a variable may be influenced by any external factors such as hardware, signal handlers, or concurrent threads. This concept of volatile is mostly used when working with systems programming, embedded systems, and multi-threaded applications.
Syntax
Here, a variable is declared volatile, which means the compiler
volatile data_type variable_name;
Example
This example represents a variable whose value may be changed from an external source. Let's assume the source is the CPU. So here, the compiler will not optimize the read operation because the variable is marked as volatile.
#include <iostream> using namespace std; int main() { // Declare a volatile variable // This can be modified by hardware or other sources volatile int countValue = 0; for(int i = 0; i < 5; i++) { cout << "Reading " << (i + 1) << ": " << countValue << endl; countValue++; } return 0; }
Output
Reading 1: 0 Reading 2: 1 Reading 3: 2 Reading 4: 3 Reading 5: 4
Use Cases of Volatile Keyword in C++
These are some of the main use-cases/applications of the volatile qualifier/keyword:
Hardware Access (Memory-Mapped I/O)
In embedded systems, programs interact with hardware using memory-mapped I/O, where certain memory addresses are linked to hardware registers. And the compilers cannot predict the external events, which may interrupt these hardware signals. Therefore, this volatile makes sure that there is correct access to memory-mapped I/O and registers.
Example
#include <iostream> using namespace std; int main() { // Here, the volatile keyword ensures that the compiler doesn't optimize away the flag variable or its accesses. volatile bool flag = false; cout << "Flag is " << (flag ? "true" : "false") << endl; flag = true; cout << "Flag is " << (flag ? "true" : "false") << endl; return 0; }
Output
Flag is false Flag is true
Signal Handlers
Signal handlers are the asynchronous functions that execute when a program receives a signal. In this, the variables which are shared between the main program and signal handlers must be accessible across all contexts, but sometimes, the shared variables can get modified at any time by the signal handlers and can interrupt the main program. So here, volatile makes sure that the shared variable is accessible to both the main program and signal handlers.
Preventing Compiler Optimizations
Most of the time, compilers implement optimizations to read the values of variables to make the code run faster. But when a variable's value changes unexpectedly, these optimizations can break the program. However, these optimizations aren't required all the time, so here volatile is used to prevent it.
Example
Here, declaring the variable volatile will force the compiler to execute each assignment within the loop without optimizing them away.
#include <iostream> using namespace std; int main() { // here the volatile will prevent the compiler from optimizing out 'temp' volatile int temp = 0; for (int i = 0; i < 1000; ++i) { temp = i; // each assignment is preserved because of volatile } cout << "Final value of temp: " << temp << endl; return 0; }
Output
Final value of temp: 999