“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register.
Examples of volatile variables are:
• Hardware registers in peripherals (for example, status registers)
• Non-automatic variables referenced within an interrupt service routine
• Variables shared by multiple tasks in a multi-threaded application
The volatile variable can be changed outside the runninf of code.for instance if one static variblae x is being used within a function but its vale is not being changed, in this case compiler will optimize the code. But if value is being changed by say CPU or some other thread, compiler can not setect that. SO to avois such condition, the volatile is introduced , which allows compiler not to optimize the code
A volatile specifier is a hint to compiler that an object may chance its value in ways not specified by the language to that aggressive optimization must be avoided.
A volatile variable is one that can change unexpectedly. Consequently, the compiler can make no assumptions about the value of the variable. In particular, the optimizer must be careful to reload the variable every time it is used instead of holding a copy in a register.
Examples of volatile variables are:
• Hardware registers in peripherals (for example, status registers)
• Non-automatic variables referenced within an interrupt service routine
• Variables shared by multiple tasks in a multi-threaded application
The volatile variable can be changed outside the runninf of code.for instance if one static variblae x is being used within a function but its vale is not being changed, in this case compiler will optimize the code. But if value is being changed by say CPU or some other thread, compiler can not setect that. SO to avois such condition, the volatile is introduced , which allows compiler not to optimize the code
quote from book “The C++ programming language”:
A volatile specifier is a hint to compiler that an object may chance its value in ways not specified by the language to that aggressive optimization must be avoided.
Leave an Answer/Comment