是否通过工会定义的行为进行原子类型?

发布于 2025-01-19 19:52:45 字数 1134 浏览 0 评论 0原文

<

typedef union
{
    uint32_t i;
    float f;
} MyUnion;

MyUnion u;
u.f = 4.2;
print("%"PRIu32, u.i); // defined behaviour

如果用于读取联合对象内容的成员与最后用于存储值的成员在 对象,该值的对象表示的适当部分被重新解释为新的对象表示 键入6.2.6中所述(有时称为“类型punning”的过程)。这可能是陷阱表示。


但是,使用原子操作时是否仍定义的行为?那是什么行为?我们有一些测序保证吗?

typedef union
{
    _Atomic uint32_t i;
    _Atomic float f; // assume sizeof(float) == sizeof(uint32_t)
} MyUnion;

int load(MyUnion* p)
{
    atomic_load(&p->i);
}

void store(MyUnion* p)
{
    return atomic_store(&p->i, 42);
}

您还可以构建奇怪的示例,例如:

typedef union
{
    _Atomic uint32_t i;
    struct
    {
        uint16_t h0; // or maybe _Atomic uint16_t ah0;
        _Atomic uint16_t ah1;
    };
} MyUnion;

6.5.2.3p5至少说:

访问原子结构或联合对象的成员会导致不确定的行为。

typedef _Atomic union
{
    uint32_t i;
    float f;
} MyUnion;

int load(MyUnion* p)
{
    return atomic_load(&p->i); // UB, also atomic_load on non-atomic type
    return p->i; // UB
}

Type-punning through unions has defined behaviour in C:

typedef union
{
    uint32_t i;
    float f;
} MyUnion;

MyUnion u;
u.f = 4.2;
print("%"PRIu32, u.i); // defined behaviour

Given via a footnote in the C Standard:

If the member used to read the contents of a union object is not the same as the member last used to store a value in the
object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new
type as described in 6.2.6 (a process sometimes called “type punning”). This might be a trap representation.


However, is it still defined behaviour when atomic operations are used? And what is that behaviour? Do we have some of the sequencing guarantees?

typedef union
{
    _Atomic uint32_t i;
    _Atomic float f; // assume sizeof(float) == sizeof(uint32_t)
} MyUnion;

int load(MyUnion* p)
{
    atomic_load(&p->i);
}

void store(MyUnion* p)
{
    return atomic_store(&p->i, 42);
}

You can also construct strange examples like:

typedef union
{
    _Atomic uint32_t i;
    struct
    {
        uint16_t h0; // or maybe _Atomic uint16_t ah0;
        _Atomic uint16_t ah1;
    };
} MyUnion;

6.5.2.3p5 at least says:

Accessing a member of an atomic structure or union object results in undefined behavior.

typedef _Atomic union
{
    uint32_t i;
    float f;
} MyUnion;

int load(MyUnion* p)
{
    return atomic_load(&p->i); // UB, also atomic_load on non-atomic type
    return p->i; // UB
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

血之狂魔 2025-01-26 19:52:45

使用原子操作时仍定义的行为吗?

适用相同的条件。它可能是陷阱表示形式,_ATOMIC对象可能在大小,表示形式,对齐方式上有所不同。如果不是陷阱表示,则结果将是(实现)定义。

is it still defined behaviour when atomic operations are used?

The same conditions apply. It may be a trap representation, and _Atomic objects may differ in size, representation, alignment compared to the unqualified types. If it is not a trap representation, then the result will be (implementation-) defined.

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