Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the volatile keyword in C language? [duplicate]

Tags:

c

volatile

People also ask

What is the volatile keyword in C?

A volatile keyword in C is nothing but a qualifier that is used by the programmer when they declare a variable in source code. It is used to inform the compiler that the variable value can be changed any time without any task given by the source code. Volatile is usually applied to a variable when we are declaring it.

What does the volatile keyword do?

volatile is a keyword known as a variable qualifier, it is usually used before the datatype of a variable, to modify the way in which the compiler and subsequent program treat the variable. Declaring a variable volatile is a directive to the compiler.

What is the use of volatile keyword in embedded C?

C's volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time--without any action being taken by the code the compiler finds nearby.

Where volatile variables are stored in C?

There's no reason for a volatile variable to be stored in any "special" section of memory. It is normally stored together with any other variables, including non-volatile ones. If some compiler decides to store volatile variables in some special section of memory - there's nothing to prevent it from doing so.


The statement "the value can be changed by means outside of this code" basically means that another program or hardware can update that variable. This is totally possible. One way of thinking of this is by relating this concept to a file that is shared among multiple programs. A file can be opened, written, and read by many programs at once. When you read from a file you want to make sure that you are reading the latest update and not the oldest.

Going back to the volatile keyword, placing volatile before a variable, in effect, does the same thing. It makes sure that what you are reading out of the variable isn't based on the compiler's optimization or an old copy of the variable that your program had. Moreover, the volatile keyword ensures that the variable is fetched from memory on every access. Therefore, both of those statements are correct regarding the volatile keyword.


C isn't necessarily for computers. For example, if you're developing for the Game Boy Advance, you often come upon memory locations that are modified by the hardware, so you might not modify the variable in your code, but it gets modified anyway. That's what volatile means.

By adding the volatile keyword, you're telling the compiler that "The value stored in this variable (memory location) might change without my code doing anything."


Consider any of the following:

  • a multi-threaded application,
  • an application using shared memory,
  • an application running on a platform that maps I/O registers into the address space,
  • an application with hardware DMA occurring in the background.

In each of these situations, memory can be altered outside the current thread.

Note that "anytime the value of a variable is change in register, then the value should affect the memory" is correct, just not very clear.


A memory location can be changed outside a programs code in a huge number of ways. For example, a DMA read from a disk can write into a buffer, or a memory mapped device can change a location because of some event on the device.


this addresses, for example, multi-threading applications: The value of a variable can be changed by several threads and thus has to be "synchronized" with the memory on each access (regardless of whether to read or write the value).