1

What happens if I declare all variables in my C code to be volatile?

As far as I know the implementation of how volatile is honored is implementation specific and the bottom-line is that the compiler cannot trust any copies of the variable it might have . Consequently, the program might be larger in size and run slower if I am to declare all variables as volatile.

Are there any other problems if I do that?

vpillai
  • 374
  • 1
  • 3
  • 15
  • 1
    The only downside to `volatile` I am aware of is what you already mentioned: the additional cycles of re-reading the value each time it is accessed. – motoku Dec 07 '14 at 02:50
  • 1
    Every time code access the variable, it must get the original value and not use a register copy. – chux - Reinstate Monica Dec 07 '14 at 02:50
  • 2
    It's not just re-loading. If the compiler figures out that a value assigned to an ordinary variable is not needed in the program, it can eliminate its computation out of existence. For a `volatile` variable, it cannot do so. – 5gon12eder Dec 07 '14 at 02:57
  • 1
    @5gon12eder that is something I was not aware of. Thank you. – motoku Dec 07 '14 at 03:07
  • possible duplicate of [What is extern volatile pointer](http://stackoverflow.com/questions/14332503/what-is-extern-volatile-pointer) – Déjà vu Dec 07 '14 at 03:28

1 Answers1

2

You should concern if you are developing drivers that read flags from control registers or pointing to that location.

Some of these register has special properties, as clearing or setting other flags just by reading them. Then using volatile would just destroy your code.

I don't think it is a good idea to declare all variables as volatile. Two of the reasons were already given: bigger code and running slower.

Worse than that is not thinking. You will be last and final professional who will look at some place and making proper programming to prevent running conditions to destroy your code. Declaring all as volatile will only postpone this to a bug you won't be able to track in the future.

Declare volatile for:

  • Shared variables
  • Optimizing your code via compilers (for old compilers... Nowadays they are pretty good already for not allowing bugs when optimizing.. But need to be explicit anyway)
  • Multithreading shared variables
rodrigogq
  • 1,943
  • 1
  • 16
  • 25
  • 1
    Many of these type of declarations are somewhat obsolete; in that they are ignored by the compiler in light of optimizations. – motoku Dec 07 '14 at 03:02
  • 1
    It is not. They are there because they are used. C is heavily used for embedded systems. Depending on the available resources, you have to use what you got to make the full of your hardware! If you write a very accessed driver, you will see a considerable performance change in your final product performance! – rodrigogq Dec 07 '14 at 03:17
  • 1
    Yes, you are correct. I was referring to the context of a beginning c programmer, working on a *simple* desktop application. I see now how I was misleading. For many situations, explicit declarations are necessary. – motoku Dec 07 '14 at 03:23