通过指针进行的访问是否会更改严格的别名语义? [英] Does access through pointer change strict aliasing semantics?

查看:78
本文介绍了通过指针进行的访问是否会更改严格的别名语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下定义:

struct My_Header { uintptr_t bits; }

struct Foo_Type { struct My_Header header; int x; }
struct Foo_Type *foo = ...;

struct Bar_Type { struct My_Header header; float x; }
struct Bar_Type *bar = ...;

说这个C代码(案例一" )是否正确:

Is it correct to say that this C code ("case one"):

foo->header.bits = 1020;

...实际上与该代码明显不同(情况二" ):

...is actually different semantically from this code ("case two"):

struct My_Header *alias = &foo->header;
alias->bits = 1020;

我的理解是它们应该有所不同:

My understanding is that they should be different:

  • 第一种情况认为分配不能影响Bar_Type中的头.仅被视为可以影响其他Foo_Type实例中的标头.

  • Case One considers the assignment unable to affect the header in a Bar_Type. It only is seen as being able to influence the header in other Foo_Type instances.

第二种情况,通过强制通过通用别名指针进行访问,将使优化程序认识到对于可能包含struct My_Header的任何类型的所有下注都是无效的.它将与通过任何指针类型的访问同步. (例如,如果您有一个Foo_Type指向实际上是一个Bar_Type的对象,它可以通过标头进行访问并可靠地找到它所拥有的-假设标头位可以告诉您.)

Case Two, by forcing access through a generic aliasing pointer, will cause the optimizer to realize all bets are off for any type which might contain a struct My_Header. It would be synchronized with access through any pointer type. (e.g. if you had a Foo_Type which was pointing to what was actually a Bar_Type, it could access through the header and reliably find out which it had--assuming that's something the header bits could tell you.)

这依赖于优化器不会变得智能",并使案例二回到案例一.

This relies on the optimizer not getting "smart" and making case two back into case one.

推荐答案

代码bar->header.bits = 1020;struct My_Header *alias = &bar->header; alias->bits = 1020;完全相同.

严格的别名规则是根据通过左值访问对象来定义的:

The strict aliasing rule is defined in terms of access to objects through lvalues:

6.5p7必须访问对象的存储值 仅由具有以下之一的左值表达式 类型:

6.5p7 An object shall have its stored value accessed only by an lvalue expression that has one of the following types:

唯一重要的是左值的类型以及由左值指定的对象的有效类型.不是,您是否将左值派生的一些中间阶段存储在指针变量中.

The only things that matter are the type of the lvalue, and the effective type of the object designated by the lvalue. Not whether you stored some intermediate stages of the lvalue's derivation in a pointer variable.

注意:自发布以下文本以来,已对问题进行了编辑.以下文字适用于malloc分配空间的原始问题,不适用于截至8月23日的当前问题.

NOTE: The question was edited since the following text was posted. The following text applies to the original question where the space was allocated by malloc, not the current question as of August 23.

关于代码是否正确.您的代码等同于 N2013 rev 1571 effective_type_9.c ,这是对现有C实现的调查,旨在起草改进的严格别名规则.

Regarding whether the code is correct or not. Your code is equivalent to Q80 effective_type_9.c in N2013 rev 1571, which is a survey of existing C implementations with an eye to drafting improved strict aliasing rules.

Q80.将结构写入malloc的区域后,是否可以通过指向具有相同叶成员类型且具有相同偏移量的不同结构类型的指针来访问其成员?

Q80. After writing a structure to a malloc’d region, can its members be accessed via a pointer to a different structure type that has the same leaf member type at the same offset?

绊脚石是代码(*bar).header.bits = 1020;是否仅设置int位的有效类型;或整个*bar.相应地,读取(*foo).header.bits是读取int,还是读取整个*foo?

The stumbling block is whether the code (*bar).header.bits = 1020; sets the effective type of only the int bits; or of the entire *bar. And accordingly, whether reading (*foo).header.bits reads an int, or does it read the entire *foo?

仅读取int并不是严格的别名冲突(可以将int读取为int).但是将Bar_Struct读取为Foo_Struct将是违反的.

Reading only an int would not be a strict aliasing violation (it's OK to read int as int); but reading a Bar_Struct as Foo_Struct would be a violation.

本文的作者认为该编写可以为整个*bar设置有效类型,尽管他们没有为此提供理由,并且我在C标准中看不到任何支持该立场的文字.

The authors of this paper consider the write to set the effective type for the entire *bar, although they don't give their justification for that, and I do not see any text in the C Standard to support that position.

在我看来,对于您的代码是否正确,目前似乎没有明确的答案.

It seems to me there's no definitive answer currently for whether or not your code is correct.

这篇关于通过指针进行的访问是否会更改严格的别名语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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