C++C++11C++14C++17C++20Learn C++Syntax

What Is The Volatile Keyword In C++?

What Is The Volatile Keyword In C++

C++ is very strong in every aspect of modern programming. Volatile types are used with the volatile keyword. It is a lesser known type qualifier that is important to read types or objects whose value can be modified at any time. The volatile keyword is useful in memory-mapped applications, generally these are from a hardware device, from a sensor, from an input device, or data from on an IoT. For example, an application reading dynamic data from the registers of a medical robot and decides what to do with that robot arm. In this post we will explain the volatile keyword in C++ and how can we use volatile type specifier.

What is volatile keyword in C++?

Volatile types are declared with the volatile keyword with a type declaration. Here’s how it is used.

We can use volatile on standard types, structs, unions. We can use them with pointers too. Here are some examples of how to use the volatile keyword in C++.

The volatile type specifier is used to change the variable whose values can be changed at any time and don’t have any constant value. The volatile qualifier is important to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Generally used in loops to read data, if you read those kinds of data without volatile types, the compiler and its optimizer can see the loop as useless. Your loop will never read the exact data in that time.

For example, in this example:

compiler optimization covert this as below:

In other words, our loop becomes infinite by being true always even if the value of x is changed in some steps inside. To solve this, we can use volatile, thus we will keep reading data from x in do-while loop. Here is the example,

If you want to keep reading x data from that address, you must use volatile specifier for the x type.

This is why the volatile specifier is needed when developing embedded systems or device drivers. If we need to read or write a memory-mapped hardware device, we can use volatile type specifier on these kinds of data. Because data on that device register could change at any time, so we need to use volatile keyword to ensure that we read data on that time and such accesses aren’t optimized away by the compiler.

Another need is some processors have floating point registers that have more than 64 bits of precision and if you need consistency then you can force each operation to go back to memory by using the volatile keyword.  

Volatile specifier is also useful for some algorithms, such as Kahan summations, etc.

Is there an example to use volatile keyword in C++?

Assume that we have an address in our device or in a library of a driver, let’s set this address to a volatile p variable and let’s check it in a loop. Here is a simple example,

Can we use volatile keyword in C++ multi-threading?

The volatile type specifier is mostly useless for multithreaded, platform-agnostic applications. It does not make operations atomic, doesn’t provide any synchronization, doesn’t create memory fences, nor does it ensure the order of execution of operations. It can be used to help the compiler when accessing some shared resource in a non-protected way. Since C++11, volatile is still not a synchronization mechanism, using volatile for threading is not recommended in general.

What Is The Volatile Keyword In C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond

C++C++11C++14C++17C++20Learn C++

What Is The Priority Queue (std::priority_queue) In Modern C++?