Volatile 'C' Implementation

Discussion in 'Programmer's Corner' started by micro84, Nov 9, 2009.

  1. micro84

    Thread Starter New Member

    Nov 8, 2009
    3
    0
    Hello Folks,

    I'm trying to understand the volatile implementation in 'C'.

    How this modifier is differed from other ?
    what is the significance of using it ?
    To which section of the memory does volatile variable get stored text or data or bss ?

    To make it clear , lets take an example.

    //variable declaration
    volatile int T;

    // main program

    While(1)
    {

    if(T1 == 4) // will be waiting for the 4 to occur
    {
    break;
    }

    }

    From the above example variable T1 defined as "volatile" , means that value changes occur externally (hw mappings ) ?


    let's consider if the volatile modifier is not used above, what would be the impact ?


    Thanks
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    An identifier with the volatile modifier has a value that may be different from the last value that was written to it. There are many reasons why this might happen, and that is a discussion for another thread. Each time the value of the identifier is required for the evaluation of an expression it must be obtained by reading it again. Any values stored in temporary locations are by definition "stale" and need to be discarded.

    A variable without this modifier works like ordinary random access memory(RAM). The value that you read from an identifier is always equal to the last value that was written to the identifier. This assumes that there is no malfunction in the meory system. Compilers can't do anything about such malfunctions.
     
  3. ftsolutions

    Active Member

    Nov 21, 2009
    48
    2
    A typical usage of 'volatile' qualifier with a varable definition - it is used with a variable that is mapped to a hardware register, such as a UART register, whose value/contents could change at any time, such as upon receipt of a new character, modem status change, overflow, input changing state, etc. Volatile forces the compiler to NOT re-use "old" value/copy of the variable if it is referenced several times in a function or in a for or while loop - it will "re-fetch" the value each time.
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Variables flagged as "volatile" are handled differently when it comes to optimization during compile.

    Variables that are modified inside interrupt service routines and then tested outside of the interrupt service routine would be another example where variables can benefit from being declared volatile.

    hgmjr
     
  5. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    The keyword "volatile" was added to the language to cover cases where a variable's value may change where it is not under the control of the compiler (such things happen with memory-mapped IO, shared memory, hardware, etc.). Without this clue to the variable's behavior, the compiler writer may do things ("optimization") that would wind up causing incorrect program behavior when the variable's value changed behind the compiler's back. In other words, it's a way of telling the compiler that it isn't in absolute control of this particular variable.
     
  6. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Most microcontroller variables (like with a PIC C compiler for example) are volatile by default. There's usually a mention somewhere in the help file.
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    That is not an accurate statement. Most variables allow you to read back what has been written. This by definition means they are NOT volatile.
     
  8. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    RB is indeed right, in any half-decent C compiler for microcontrollers the ports and special function registers are declared as volatile.

    The volatile specifier has nothing to do with whether you can read back what has been written but rather (as others have stated) tells the compiler that the variable can change outside of the program's influence so the compiler had better
    a: read the value each time before using it and
    b: not optimise the variable out (remove) just because it can't see it being varied in the program.
     
    Last edited: Nov 27, 2009
  9. Papabravo

    Expert

    Feb 24, 2006
    10,136
    1,786
    I beg to differ. I agree that registers and special function registers are defined as volatile precisely because it is the case that they do not behave like RAM. That is, you cannot depend on reading back the last thing that you wrote.

    The crux of my previous response depends on the ratio of RAM variables to special function registers. In my experience, the number of RAM variables almost always exceeds the number of registers. But to each his own.
     
  10. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    To get back to the OP's questions... The keyword volatile was introduced in the late 1980's; in K&R, 2nd ed., section A8.2:
    The purpose of volatile is to force an implementation to suppress optimization that could otherwise occur.
    That's the crux of the matter, but it is a bit sterile unless you're familiar with the reason and need for it. To reiterate, it is needed in cases where a variable's value can "change behind the back of a compiler".

    It is important to note that the compiler writer can choose to ignore this keyword. While that's apparently allowed by the C standard (I don't have a copy), it wouldn't make the compiler too popular with users in the embedded space. :)

    Note that volatile was introduced in the same standard as const was. These "type qualifiers" are used to indicate special properties of variables. If you understand the definitions, then that's the only way they differ (to answer your first question).

    As to significance of using it, from a practical perspective, it's the programmer's way of stating that a variable may change from influences other than the compiler. Memory-mapped IO and shared memory are common reasons.

    These type specifiers are "orthogonal" to storage class specifiers. Thus, knowing that a variable is volatile says nothing about where it is stored.

    Finally, in your example, exactly what happens is up to the compiler writer. Again, by using the word volatile, the programmer is indicating that the variable may change by mechanisms other than the compiler (i.e., the code that one explicitly sees in the program).

    In your sample code, it's impossible to predict what will happen because T1's value is undefined. The keyword volatile implies that T1 may change by mechanisms other than the program's.
     
Loading...