仅包含标识符的表达式是否会读取易失性变量?
我正在使用 Microchip 的编译器为微控制器(dsPIC24)编写代码。外围设备都是内存映射变量。有时需要通过读取缓冲区来清除缓冲区,我通常这样做:
SPI1BUF;
...其中 SPI1BUF
在 Microchip 的标头中声明为 volatile char
。它对我有用,但现在我很好奇:这是标准化行为吗?我记得如果我执行一个赋值:
unsigned char x = SPI1BUF;
...标准规定必须读取易失性变量。但是对于标识符作为整体表达式的情况也是如此吗?
I'm writing for a microcontroller (dsPIC24) using Microchip's compiler. The peripheral devices are all memory-mapped variables. Occasionally it is necessary to clear a buffer by reading from it, which I usually do with just:
SPI1BUF;
...where SPI1BUF
is declared volatile char
in Microchip's headers. It works for me, but now I'm curious: is it standardised behaviour? I recall that if I perform an assignment:
unsigned char x = SPI1BUF;
...the standard says that the volatile variable must be read. But is this also true for the identifier-as-the-whole-expression case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种裸表达式是C标准中称为“表达式语句”的语句类型。标准(6.8.3)的相关部分说:
读取
volatile
限定变量的值被认为是一种副作用,因此根据第 §5.1.2.3 节,它不能被忽略:假设 SPI1BUF 是左值(如果它是标识符,或者扩展为调用一元
*
运算符的宏,则都是如此),§6.3.2.1有这样说:由于没有任何异常适用,这确保了底层(易失性)对象是“已访问”的,因为左值已转换为存储在对象中的值,这显然需要访问那个物体。
总之:是的,这是标准行为。
Such a bare expression is a statement type called an "expression statement" in the C standard. The relevant section of the standard (6.8.3) says:
Reading the value of a
volatile
-qualified variable is considered such a side effect, so it can't be elided, per §5.1.2.3:Assuming that
SPI1BUF
is an lvalue (this is true both if it is an identifier, or a macro that expands to an invocation of the unary*
operator), §6.3.2.1 has this to say:Since none of the exceptions apply, this assures us that the underlying (volatile) object is "Accessed", because the lvalue has been converted to the value stored in the object, which obviously necessitates accessing the value of that object.
In summary: yes, this is standard behaviour.