Search code examples
cwarningsvolatile

Is it possible to "Turn off" or "Turn on" volatility in C variables?


I have two static volatile variables in C and I would like to check them both in a logical statement. However, when I do I get a warning " undefined behavior: the order of volatile accesses is undefined in this statement 1037" Is it possible to suspend volatility of a C variable for a very brief moment to ensure good data?

Here is the code:

static volatile unsigned char b;
static volatile unsigned char a;

//update the states of the two volatile variables 
update_vars( &a);
update_vars( &b);

// check them in a logical statement
// Can I suspend the volatile lable??  
if((addr_bit & (a | b)) == 0){
// update another variables
}
else{
// another action
}

I'm thinking about this in the same context of interrupts, whereas you temporary suspend them if you want stable evaluation of the data at a precise moment. Thanks!


Solution

  • The volatile property of a variable cannot be disabled.

    You need to create a non-volatile copy of each and then operate on those.

    unsigned char a_stable = a;
    unsigned char b_stable = b;
    
    if((addr_bit & (a_stable | b_stable)) == 0){
        ...