挥发性结构体语义学 [英] Volatile Struct Semantics
问题描述
是否足以声明结构类型的变量的一个实例挥发性(如果字段在重入code访问),或者必须在一个声明结构的特定字段挥发性?
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;
和
typedef struct{
volatile uint8_t bar;
} foo_t;
foo_t foo_inst;
我承认,声明指针类型的变量挥发性(例如挥发性uint8_t有* foo的)只是通知的地址指向的由富可能会改变,同时使没有语句有关值由富指向的编译器。目前还不清楚我的比喻是否适用于结构类型的变量。
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.
推荐答案
在你的榜样,两者是相同的。但是,问题围绕指针。
In your example, the two are the same. But the issues revolve around pointers.
首先,挥发性uint8_t有* foo的;
告诉编译器内存被指向的波动。如果你想标记指针本身挥发性,你需要做的 uint8_t有* foo的波动;
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 do uint8_t * volatile foo;
这就是你在哪里得到标记结构作为挥发性VS标记单个字段三者之间的主要差异。如果你有:
And that is where you get to the main differences between marking the struct as volatile vs marking individual fields. If you had:
typedef struct
{
uint8_t *field;
} foo;
volatile foo f;
这会像:
typedef struct
{
uint8_t * volatile field;
} foo;
和不喜欢的:
typedef struct
{
volatile uint8_t *field;
} foo;
这篇关于挥发性结构体语义学的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!