gpt4 book ai didi

c++ - 通过重新解释转换创建无效引用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:05 25 4
gpt4 key购买 nike

我正在尝试确定以下代码是否调用了未定义的行为:

#include <iostream>

class A;

void f(A& f)
{
char* x = reinterpret_cast<char*>(&f);
for (int i = 0; i < 5; ++i)
std::cout << x[i];
}

int main(int argc, char** argue)
{
A* a = reinterpret_cast<A*>(new char[5])
f(*a);
}

我的理解是 reinterpret_castchar* 是兼容的,因为标准允许使用 charunsigned char 别名 指针(强调我的):

If a program attempts to access the stored value of an object through an lvalue of other than one of the following types the behavior is undefined:

  • the dynamic type of the object,
  • a cv-qualified version of the dynamic type of the object,
  • a type that is the signed or unsigned type corresponding to the dynamic type of the object,
  • a type that is the signed or unsigned type corresponding to a cv-qualified version of the dynamic type of the object,
  • an aggregate or union type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union),
  • a type that is a (possibly cv-qualified) base class type of the dynamic type of the object,
  • a char or unsigned char type.

但是,我不确定 f(*a) 是否通过创建对无效指针的 A& 引用来调用未定义的行为。决定性因素似乎是“尝试访问”措辞在 C++ 标准上下文中的含义。

我的直觉是,这构成访问,因为访问需要定义A(它已声明,但在此示例中未定义)。不幸的是,我在 C++ 标准中找不到“访问”的具体定义:

f(*a) 会调用未定义的行为吗?什么构成了 C++ 标准中的“访问”?

我明白,无论答案如何,在生产代码中依赖这种行为可能不是一个好主意。我问这个问题主要是为了提高我对这门语言的理解。

[编辑] @SergeyA 引用了标准的这一部分。我将其包含在此处以便于引用(强调我的):

5.3.1/1 [expr.unary.op]

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.” [Note: indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1. — end note ]

追踪对4.1的引用,我们发现:

4.1/1 [conv.lval]

A glvalue (3.10) of a non-function, non-array type T can be converted to a prvalue. If T is an incomplete type, a program that necessitates this conversion is ill-formed. If T is a non-class type, the type of the prvalue is the cv-unqualified version of T. Otherwise, the type of the prvalue is T.

When an lvalue-to-rvalue conversion is applied to an expression e, and either:

  • e is not potentially evaluated, or
  • the evaluation of e results in the evaluation of a member ex of the set of potential results of e, and ex names a variable x that is not odr-used by ex (3.2)

the value contained in the referenced object is not accessed.

我认为我们的答案在于 *a 是否满足第二个要点。我在解析该条件时遇到问题,所以我不确定。

最佳答案

char* x = reinterpret_cast<char*>(&f);已验证。或者,更具体地说,通过 x 访问允许 - 转换本身始终有效。

A* a = reinterpret_cast<A*>(new char[5])无效 - 或者,准确地说,通过 a 访问将触发未定义的行为。

原因是虽然可以通过 char* 访问对象,通过随机对象访问字符数组是不行的。标准允许第一个,但不允许第二个。

或者,通俗地说,您可以为 type* 添加别名通过char* , 但你不能别名 char*通过type* .

编辑

我只是注意到我没有回答直接的问题(“C++ 标准中的“访问”是什么”)。显然,标准没有定义访问(至少,我找不到正式的定义),但取消引用指针通常被理解为有资格访问。

关于c++ - 通过重新解释转换创建无效引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36457841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com