You are here:

C++/volatile modifiers



hello sir

i have read the concept of volatile modifiers from net. but the image of volatile modifiers is not clear in my mind. can u tell me

Q------> what is volatile modifiers ?

Q------>what is the difference between a volatile variable & non volatile varilable?

Q----> where should i use it or where not?

thank you

What is the volatile modifier?
The volatile modifier is like the const modifier. The term used in the C++ standard is qualifier rather than modifier. Const and volatile are qualifications that can be applied to types. In fact where you can use const you can use volatile so a type can be neither const nor volatile qualified, const qualified, volatile qualified or const volatile qualified. The language is so regular in this respect that the standard uses the terms like 'cv-qualifiers' and 'cv-qualified', cv being short hand for const-volatile.

What is the difference between a volatile and non volatile variable?
A volatile qualification on a type is a hint to the compiler that objects (variables) of that type are likely to change or be read outside of the control of the program under compilation, hence the compiler cannot assume anything with regard to optimisation.

Where should I use it or where not?
In nearly all standard application code there is no reason to use volatile. The exceptions are where you are interfacing with devices or possibly certain situations involving multithreaded code (although volatile is no help on its own with synchronisation problems).

For example if you have some object that is mapped to some memory mapped device register and this device's documentation states you have to write zero twice to this register for it to perform some action you would write some code like the following:

   *pRegister = 0;
   *pRegister = 0;

Now if the type pointed to is not volatile then the compiler is free to say: "Hang on this code is stupid, I'll just remove one of the writes of zero to this location, the result will be the same". By qualifying the type of the register as volatile the compiler will say "Hmm, this location is volatile, this means there are things going on here I am not aware of, I'll leave well alone and assume they know what they are doing".

Volatile can be used with function overloading. Just as a const qualified type passed to a function is different for overloading purposes to a cv-unqualified type, so is a volatile and const volatile qualified type. The same goes for class instance member functions: you can have cv-unqualified and const qualified member functions (as is quite common), but you can also have volatile qualified and const-volatile qualified instance member functions, and you can overload on all these qualification variations.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.

[an error occurred while processing this directive]