volatile short

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxlandoz
    New Member
    • Jun 2007
    • 1

    volatile short

    Could anybody help me to understand what a volatile short variable should be used for? Whats the difference with short?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by maxlandoz
    Could anybody help me to understand what a volatile short variable should be used for? Whats the difference with short?
    A variable should be declared volatile whenever its value can be changed by something beyond the control of the program in which it appears.The compiler needs to know that this value may be changed by some external force when it optimizes the code. The declaration without volatile behaves as normal short type variable

    Regards

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Another way of saying this is that since the value of the variable may change ouside the control of the compiler, the compiler should make no assumptions about it. Like, put it in a register instead of making it a variable. volatile tells the compiler be dang sure you create a variable.

      Comment

      • dumparun
        New Member
        • Feb 2007
        • 26

        #4
        Originally posted by weaknessforcats
        Another way of saying this is that since the value of the variable may change ouside the control of the compiler, the compiler should make no assumptions about it. Like, put it in a register instead of making it a variable. volatile tells the compiler be dang sure you create a variable.
        Another reason i think is to avoid the intelligent compilers from skipping the dead code.
        a scenario where a variable is declared and not explicitly used, will be removed by the compiler. But when "volatile" keyword is used its not the case

        Comment

        Working...