是否从指针到常量去除const的服从用C严格走样,并参考同一个对象? [英] Does removing const from a pointer-to-const obey strict aliasing in C, and refer to the same object?

查看:152
本文介绍了是否从指针到常量去除const的服从用C严格走样,并参考同一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问在C以下code定义的行为?

Does the following code in C have defined behavior?

int main() {
    const int i = 0;
    return *(int*)(&i);
}

我问,因为6.5 / 7列表作为一个有效的别名的有效对象的类型兼容的类型的合格的版本。但有效对象的类型是 const int的,我不认为 INT 是一个合格的版本 const int的(虽然正好相反)。无论是 INT const int的兼容(6.7.3 / 10)。

I ask because 6.5/7 lists "a qualified version of a type compatible with the effective type of the object" as a valid alias. But the effective type of the object is const int, and I don't think int is a qualified version of const int (although the reverse is true). Neither are int and const int compatible (6.7.3/10).

此外,6.3.2.3/2说,你可以通过转换指针类型的添加的预选赛,而所得到的指针是相等的。 6.3.2.3/7说,你可以将任意两个指针类型(因此投(INT *)(安培; I)本身是允许的)。但并不是说所产生的指针指到同一对象甚至可以说,它是平等的。所有它说的是,它可以转换回原来的类型(在这种情况下, const int的* )。也就是说,即使别名是合法的,目前还不清楚,我认为标准的保证,我的指针转换确实导致引用 A指针i

Furthermore, 6.3.2.3/2 says that you can convert pointer types by adding qualifiers, and that the resulting pointer is equal. 6.3.2.3/7 says that you can convert any two pointer types (so the cast (int*)(&i) itself is permitted). But doesn't say that the resulting pointer refers to the same object or even that it is equal. All it says is that it can be converted back to the original type (in this case const int*). That is, even if the alias is legal it's not clear to me that the standard guarantees that my pointer conversion does indeed result in a pointer that refers to i.

所以,做标准实际上定义我的code的行为,如果是的话这是哪里定义的?

So, does the standard actually define the behavior of my code, and if so where is this defined?

我知道,code在实践工作。我心里有一个上它不工作假设(和离奇)实现。我可以问,落实是否符合标准(如果没有违反,哪些部分),但是我不想,如果有在我的想象中实现不符合其他方面来把水搅浑。如果有人认为这将有助于他们回答这个问题,我将描述实现。

I'm aware that the code works in practice. I have in mind a hypothetical (and bizarre) implementation on which it doesn't work. I could ask whether that implementation conforms to the standard (and if not, what part it violates), but I don't want to muddy the waters if there are other respects in which my imagined implementation fails to conform. I will describe the implementation if anyone thinks it will help them answer the question.

推荐答案

这是至少的隐含的,它的作品,由§6.7.3P5:

It is at least implied that it works, by §6.7.3 p5:

如果试图修改与一个定义的对象
  通过使用一个左的非const限定的const限定型
  类型,行为是不确定的。如果试图引用一个
  通过使用一个左值与volatile限定类型的对象定义
  具有非易失性限定的类型,其行为是不确定的。

If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.

请注意,对于volatile限定类型上面说的参考的,但是对于const限定的类型,它只是说的修改的,这意味着非修改访问是OK (来证明这一规则的例外的)。

Note that for volatile-qualified types it says refer to, but for const-qualified types it just says modify, which implies that a non-modifying access is OK ("the exception that proves the rule").

它看起来像你对我可能已经确定了标准的缺陷,虽然。

It looks to me like you may have identified a defect in the standard, though.

这篇关于是否从指针到常量去除const的服从用C严格走样,并参考同一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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