易失性结构语义

发布于 2024-08-17 11:08:01 字数 400 浏览 8 评论 0原文

是否足以将结构类型变量的实例声明为易失性(如果在可重入代码中访问其字段),或者必须将结构的特定字段声明为易失性?

换句话说,以下之间的语义差异(如果有)是什么:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2024-08-24 11:08:01

在你的例子中,两者是相同的。但问题围绕着指针。

首先,volatile uint8_t *foo; 告诉编译器所指向的内存是易失性的。如果您想将指针本身标记为易失性,则需要执行 uint8_t * volatile foo;

这就是将结构标记为易失性与标记单个字段之间的主要区别的地方。如果你有:

typedef struct
{
    uint8_t *field;
} foo;

volatile foo f;

那会像:

typedef struct
{
    uint8_t * volatile field;
} foo;

而不是像:

typedef struct
{
    volatile uint8_t *field;
} 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 do uint8_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:

typedef struct
{
    uint8_t *field;
} foo;

volatile foo f;

That would act like:

typedef struct
{
    uint8_t * volatile field;
} foo;

and not like:

typedef struct
{
    volatile uint8_t *field;
} foo;
半世晨晓 2024-08-24 11:08:01

如果你声明一个带有 易失性的结构,那么它的所有成员也将是易失性的

if you declare a structure with volatile then all its members will also be volatile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文