它是UB抛掷const和阅读价值? [英] Is it UB to cast away const and read value?

查看:198
本文介绍了它是UB抛掷const和阅读价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

澄清:我的问题是:


  • 是否UB使用类型的左值 INT 来访问有效键入的对象const int的 <? / LI>
  • Is it UB to use an lvalue of type int to access an object of effective type const int ?

这问题有哪些使用类型的左值 INT 双code样品来访问有效键入 const int的,我的意图是用尽可能少分心尽可能实现这一目标。如果有UB的任何其它来源除了这一特定问题请留言,我会尝试更新code样本。

This question has two code samples which use an lvalue of type int to access an object of effective type const int, and my intent is to achieve this with as little distraction as possible. If there is any other source of UB besides this specific problem please leave a comment and I will try to update the code sample.

下面是一个讨论的具体code例如:

Here is a specific code example for discussion:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    const int c = 5;

    printf("%d\n", *(int *)&c);
}

我想可能是UB的原因是,严格别名规则似乎是说,这是UB:

The reason I think it might be UB is that the strict aliasing rule seems to say that it is UB:

C11 6.5 / 7

对象应具有其存储的值只能由具有之一的左值前pression访问
  以下类型:

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


      
  • 一个类型的有效对象的类型,
  • 兼容
      
  • 一个合格的版型与有效对象的类型兼容,

  •   
  • ...

  •   

有效对象的类型的(6.5 / 6)这里是 const int的

The effective type of the object (6.5/6) here is const int.

第一点: INT const int的不是的兼容类型的(6.2 0.7 / 1,6.7.3 / 10)。

First bullet point: int and const int are not compatible types (6.2.7/1, 6.7.3/10).

第二个项目符号点: INT 似乎并不为 const int的的一个合格的版本,因为我们没' ŧ加入预选赛生产。然而6.2.5 / 26不清楚:

Second bullet point: int does not seem to be a qualified version of const int, because we didn't produce it by adding qualifiers. However 6.2.5/26 is unclear:

每个非限定类型都有它类型的几个合格版本,对应于一个,两个,或所有三个常量,挥发性的,并限制限定符的组合。一种类型的合格的或不合格的版本是属于同一类型类别,具有相同的重presentation和对齐要求不同的类型。派生类型不被限定符合格(如果有的话)从其所衍生的类型的

Each unqualified type has several qualified versions of its type, corresponding to the combinations of one, two, or all three of the const, volatile, and restrict qualifiers. The qualified or unqualified versions of a type are distinct types that belong to the same type category and have the same representation and alignment requirements. A derived type is not qualified by the qualifiers (if any) of the type from which it is derived.

它并没有定义什么是 const int的合格版本将是,它在应用到不合格的类型只定义了所谓合格的版本。

It doesn't define what a "qualified version of const int" would be, it only defines the term "qualified version" when applied to an unqualified type.

二code样品:<​​/ STRONG>

Second code sample:

int *pc = malloc(sizeof *pc);
memcpy(pc, &c, sizeof c);
printf("%d\n", *pc);   // UB?

由于的memcpy preserves有效的类型(6.5 / 6),通过 * PC 正好有读书与严格别名规则相同的交互通过 *为(int *)及阅读; C。做第一个例子

Since memcpy preserves the effective type (6.5/6) , reading through *pc has exactly the same interaction with the strict aliasing rule as reading through *(int *)&c does in the first example.

推荐答案

这是不。你所发现的是,为什么它不能投隐含

It is not. What you have found is why it cannot be implicitely cast.

[6.2.5 / 26]指出:

[6.2.5/26] states:

每个非限定类型都有它类型的几个合格版本,对应于一个,两个,或所有三个常量,挥发性的,并限制限定符的组合。
  一种类型的合格的或不合格的版本是属于同一类型类别,具有相同的重presentation和对齐要求不同的类型。

Each unqualified type has several qualified versions of its type, corresponding to the combinations of one, two, or all three of the const, volatile, and restrict qualifiers. The qualified or unqualified versions of a type are distinct types that belong to the same type category and have the same representation and alignment requirements.

(注:每个不合格键入 const int的不是不合格,但 INT 不合格。)

(Note: each unqualified type. const int is not unqualified but int is unqualified.)

使用脚注:

同样重presentation和对齐要求是为了暗示互换性作为函数的参数,从函数返回值,和工会的成员。

The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.

这意味着读它会以同样的方式,并产生相同的值。

This means reading it will work the same way and yield the same value.

[6.7.3 / 6]规定UB只进行修改:

[6.7.3/6] specifies UB only for modifications:

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

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.

这篇关于它是UB抛掷const和阅读价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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