Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can an auto variable be volatile?

Tags:

c

I know about volatile variables that are defined at file scope. The compiler isn't allowed to make assumptions about these variables. They may be changed at nearly any time and the compile must not optimize out reads of the variable.

Now I found this code

BOOL InstallHandler()
{
    volatile BOOL b_bulk_erase = FALSE;
    volatile BOOL b_test_read_write = FALSE;
    volatile BOOL b_continue = TRUE;
    ...
    if (b_test_read_write)
    {
        read();
        write();
    }
}

How does the volatile corresponds to variables to the stack i.e. owned by one thread?

Edit:

With owned by one thread I wanted to express that the variable is not exposed. The address isn't given to anything else. It is not used by any other thread.

like image 279
harper Avatar asked Jan 11 '23 03:01

harper


1 Answers

Ok, I realized the intention for that volatile.

The function is part of a device driver that communicates with a hardware device. The functions read and write are not used during normal operation.

But when the developer runs the program in a debugger she can set a breakpoint at the if clause and change the variable with the debugger. This will allow to manipulate the execution and to call to read and write.

The volatile inhibits the optimization that would be possible since the if clause tests a const expression. Without the volatile the whole if probably wouldn't be in the code.

like image 52
harper Avatar answered Jan 20 '23 13:01

harper