Home > Enterprise >  when volatile keyword is used to qualify a variable ,were is it stored?
when volatile keyword is used to qualify a variable ,were is it stored?

Time:02-02

In embedded systems volatile is used to let the compiler not to modify/optimize the variable, however what is the significance of using volatile for a local variable , when used for global variable it reside in data segment , what is purpose if used in local variable and will it be in stack.

CodePudding user response:

First of all, scope and storage duration are different things. Just because something is declared at local scope, it doesn't necessarily have automatic storage duration, which is the correct term to use here. Meaning that where a local scope variable is allocated is handled automatically by the compiler.

There is no direct relation between volatile and where a variable is stored. Except volatile access implies that a memory access is necessary. Most often the compiler will not store automatic storage volatile variables in registers but instead on the stack, since they get an address that way. But none of this is required by the C standard.

Similarly, volatile variables need to be allocated in a region of memory which isn't pre-fetched to data cache, since caching will conflict with the value update upon access. Who is responsible to ensure this isn't always clear, the responsibility lands somewhere between the compiler and the programmer.

As for the purpose of using automatic storage volatile variables, there are many. Communicating with hardware registers, enforcing blocks against optimization on critical code, ensuring that a variable gets allocated on the stack so that it becomes watchable in a debugger etc etc.

CodePudding user response:

The volatile keyword has the same purpose when used in local variables as in global variables. The data will be stored in just the same manner as without the volatile keyword.

The purpose of volatile is always to avoid any compiler optimizations which might cause the variable not to be written or read from memory. The typical usage for global variables is of course if they are accessed from an interrupt for example, or written to or read by hardware (DMA or similar).

But there are good reasons for usage in local variables as well, for example a common way of implementing a small delay is something like this:

for(int i=0; i<100; i  );

This code can be seen as useless, since the value of i is never used. Thus, with compiler optimizations activated, the compiler may choose to completely remove that loop. After all, that would be a good optimization as the loop only consumes time and does nothing really useful.

If you instead mark the variable i as volatile, the compiler will be forced into actually writing all those values to i so it doesn't remove the delay-loop.

Another common usage of volatile for local variables is if a pointer of said variable is passed to some function which uses it in the same way as the common usages of global variables.

  •  Tags:  
  • Related