繁体   English   中英

是否可以“关闭”或“打开” C变量的波动性?

[英]Is it possible to “Turn off” or “Turn on” volatility in C variables?

我在C中有两个静态的volatile变量,我想在一个逻辑语句中检查它们两个。 但是,当我收到警告时,“未定义行为:此语句1037中未定义易失性访问的顺序”是否可以在很短的时间内暂停C变量的波动性以确保获得良好的数据?

这是代码:

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
}

我在相同的中断环境中考虑此问题,但是如果希望在准确的时刻对数据进行稳定的评估,则可以暂时将其挂起。 谢谢!

不能禁用变量的volatile属性。

您需要创建每个的非易失性副本,然后对其进行操作。

unsigned char a_stable = a;
unsigned char b_stable = b;

if((addr_bit & (a_stable | b_stable)) == 0){
    ...

为了避免警告,您可以分解C语句,以使每个C语句仅具有对volatile变量的一次访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM