易失性结构语义
是否足以将结构类型变量的实例声明为易失性(如果在可重入代码中访问其字段),或者必须将结构的特定字段声明为易失性?
换句话说,以下之间的语义差异(如果有)是什么:
typdef struct {
uint8_t bar;
} foo_t;
volatile foo_t foo_inst;
并且
typedef struct{
volatile uint8_t bar;
} foo_t;
foo_t foo_inst;
我认识到将指针类型变量声明为易失性(例如易失性 uint8_t * foo)只是通知编译器 foo 指向的地址可能会更改,同时使没有关于 foo 指向的值的声明。我不清楚这种类比是否适用于结构类型变量。
Is it sufficient to declare an instance of a structure-typed variable as volatile (if its fields are accessed in re-entrant code), or must one declare specific fields of the structure as volatile?
Phrased differently, what are the semantic differences (if any) between:
typdef struct {
uint8_t bar;
} foo_t;
volatile foo_t foo_inst;
and
typedef struct{
volatile uint8_t bar;
} foo_t;
foo_t foo_inst;
I recognize that declaring a pointer-typed variable as volatile (e.g. volatile uint8_t * foo) merely informs the compiler that the address pointed-to by foo may change, while making no statement about the values pointed to by foo. It is unclear to me whether an analogy holds for structure-typed variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的例子中,两者是相同的。但问题围绕着指针。
首先,
volatile uint8_t *foo;
告诉编译器所指向的内存是易失性的。如果您想将指针本身标记为易失性,则需要执行 uint8_t * volatile foo;这就是将结构标记为易失性与标记单个字段之间的主要区别的地方。如果你有:
那会像:
而不是像:
In your example, the two are the same. But the issues revolve around pointers.
First off,
volatile uint8_t *foo;
tells the compiler the memory being pointed to is volatile. If you want to mark the pointer itself as volatile, you would need to douint8_t * volatile foo;
And that is where you get to the main differences between marking the struct as volatile vs marking individual fields. If you had:
That would act like:
and not like:
如果你声明一个带有 易失性的结构,那么它的所有成员也将是易失性的
if you declare a structure with volatile then all its members will also be volatile