+14

What is Volatile & Mutable keywords in Cpp?

Please help me to understand the Volatile keyword & mutable keyword in C++(with short intro example).... Thanks in Advance 🙂

2/20/2020 7:55:56 PM

SuDeep Jaiswal

4 Answers

New Answer

+4

'mutable' keyword is used for cpp class member. To understand this 'mutable' keyword, we need to understand cpp const class member function first: (quote from https://www.tutorialspoint.com/const-member-functions-in-cplusplus) ----------------------------------------------------------------------------------------------------- The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object. Non-const functions can be called by non-const objects only. Here is the syntax of const member function in C++ language: datatype function_name const(); ----------------------------------------------------------------------------------------------------- So const member function cannot modify class member, except if this member is 'mutable'. Example as below: (https://code.sololearn.com/cg2eup0lkhgo/#cpp) #include <iostream> class Base{ mutable int aMutable; int NotMutable; public: Base(int a=0): aMutable(a), NotMutable(a) {} void SetData(int a) const { this->aMutable= a; // compile OK // this->NotMutable = a; // compile error } int GetData() const { return this->aMutable; } }; int main(){ Base obj(7); obj.SetData(4); std::cout << obj.GetData(); return 0; } For 'volatile' keyword, this is a good reference: https://www.geeksforgeeks.org/understanding-volatile-qualifier-in-c/ In short: The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Example code: https://code.sololearn.com/cxGPVXZa4dgq/#c, which have the same output as in the link using my Vscode msys64 compiler.

+7

volatile means that variable can be changed between accesses to it. Usually it is changed by hardware and the compiler does not know about that changes, for example it may be some control register of hardware that mapped to the memory. It tells the compiler do not optimize accesses to that variable. Example: without volatile: int a; a = 5; // that line may be optimized out by compiler // because the next line overrides value in the variable a; a = 7; int b = a; with volatile: volatile int a; a = 5; // compiler may not optimize out that line because variable is volatile a = 7; int b = a; another example without volatile int flag = 0; while(!flag); // compiler will read flag only once. // it will not read flag on each iteration. // it will be infinite loop with volatile: volatile int flag = 0; while(!flag); // compiler will do read flag on each iteration // because it volatile, it may be change somehow // this code sometimes used to wait some event on hardware // continuous reading is called polling // Number and sequence of accesses (read/write) to the hardware registers is very important. It will be a huge problem if the compiler begins to optimize out data that is sent trough communication channel (data is written to the same register) usually volatile variables has predefined address mutable is used with classes. It allows changing members of the class from its const methods; If a member of a class does not defined as mutable any change of it from const method will generate compile error. but if a member is defined as mutable you can change it from const method (i.e. , mutable members of const objects may be changed)

+3

The compiler does a bunch of optimizations to your code, to make it more efficient. Some of these optimizations mean "ignoring" certain part of your code, since it doesn´t change anything. According to wiki, "Volatile" tells the compiler not to optimize that part of the code, since, even though it doesn´t seem to change and could be optimized, it could be changed by external sources and, therefore, cause unwanted behaviour if you optimize it. Read this article: https://en.wikipedia.org/wiki/Volatile_(computer_programming) And here you have excelent examples: https://stackoverflow.com/questions/4437527/why-do-we-use-volatile-keyword

+2

@Fernando Pozzetti No, it is not correlated to mutable. mutable is only used with classes and lambda-expressions (since c++11) and has absolutely different meaning.