将原子类型的指针分配给非原子类型的指针 [英] Assigning pointers to atomic type to pointers to non atomic type

查看:60
本文介绍了将原子类型的指针分配给非原子类型的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码的行为是否定义明确?

#include <stdatomic.h>

const int test = 42;
const int * _Atomic atomic_int_ptr;
atomic_init(&atomic_int_ptr, &test);
const int ** int_ptr_ptr = &atomic_int_ptr;
printf("int = %d\n", **int_ptr_ptr); //prints int = 42


我将原子类型的指针分配给非原子类型的指针(类型相同).这是我对这个示例的看法:


I assigned a pointer to atomic type to a pointer to non-atomic type (the types are the same). Here are my thoughts of this example:

标准明确规定了 const volatile restrict 限定词与 _Atomic 限定词的区别6.2.5(p27):

The Standard explicitly specify distinction of const, volatile and restrict qualifiers from the _Atomic qualifier 6.2.5(p27):

本标准明确使用短语原子的,合格的或不合格的类型",只要允许使用该类型的原子版本以及其他类型的合格版本.词组'合格或不合格类型'",但没有具体提及原子,不包括原子类型.

this Standard explicitly uses the phrase ‘‘atomic, qualified or unqualified type’’ whenever the atomic version of a type is permitted along with the other qualified versions of a type. The phrase ‘‘qualified or unqualified type’’, without specific mention of atomic, does not include the atomic types.

此外,合格类型的兼容性定义为 6.7.3(p10):

Also the compatibility of qualified types is defined as 6.7.3(p10):

要使两个合格的类型兼容,两个都应具有兼容类型的完全限定版本;的顺序限定符或限定符列表中的类型限定符不影响指定的类型.

For two qualified types to be compatible, both shall have the identically qualified versionof a compatible type; the order of type qualifiers within a list of specifiers or qualifiers does not affect the specified type.

结合以上引用,我得出结论,原子类型和非原子类型是兼容的类型.因此,应用简单分配 6.5.16.1(p1)(临时矿)的规则:

Combining the quotes cited above I concluded that atomic and non-atomic types are compatible types. So, applying the rule of simple assigning 6.5.16.1(p1) (emp. mine):

左操作数具有原子,合格或不合格的指针类型,以及(考虑到左操作数应具有的类型左值转换后)两个操作数都是限定 的指针兼容类型的 或不合格版本,以及由左侧具有右侧所指类型的所有限定词;

the left operand has atomic, qualified, or unqualified pointer type, and (considering the type the left operand would have after lvalue conversion) both operands are pointers to qualified or unqualified versions of compatible types, and the type pointed to by the left has all the qualifiers of the type pointed to by the right;

因此,我得出的结论是,行为已得到很好的定义(即使将原子类型分配为非原子类型也是如此).

So I concluded that the behavior is well defined (even in spite of assigning atomic type to a non-atomic type).

所有问题在于,应用上述规则,我们还可以得出以下结论: 将非原子类型简单分配给原子类型 显然不正确,因为我们有专门的通用 atomic_store 函数.

The problem with all that is that applying the rules above we can also conclude that simple assignment a non-atomic type to an atomic type is also well defined which is obviously not true since we have a dedicated generic atomic_store function for that.

推荐答案

6.2.5p27 :

此外,还有_Atomic限定词._Atomic的存在限定词指定原子类型.大小,表示形式和原子类型的对齐方式不必与原子类型的对齐方式相同对应的不合格类型.因此,本标准明确只要一个类型的原子版本与其他合格版本一起被允许类型的版本.短语合格或不合格类型",没有具体提及原子,不包括原子类型.

Further, there is the _Atomic qualifier. The presence of the _Atomic qualifier designates an atomic type. The size, representation, and alignment of an atomic type need not be the same as those of the corresponding unqualified type. Therefore, this Standard explicitly uses the phrase ''atomic, qualified or unqualified type'' whenever the atomic version of a type is permitted along with the other qualified versions of a type. The phrase ''qualified or unqualified type'', without specific mention of atomic, does not include the atomic types.

我认为这应该明确表明,原子限定的类型被认为与它们所基于的类型的合格或不合格版本兼容.

I think this should make it clear that atomic-qualified types are not deemed compatible with qualified or unqualified versions of the types they're based on.

这篇关于将原子类型的指针分配给非原子类型的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆