gpt4 book ai didi

c++ - 允许使用 char* 别名 T*。反过来也允许吗?

转载 作者:行者123 更新时间:2023-12-02 01:52:43 24 4
gpt4 key购买 nike

注:这个问题已被重命名和减少,以使其更具针对性和可读性。大多数评论都引用了旧文本。

根据标准,不同类型的对象不能共享相同的内存位置。所以这不合法:

std::array<short, 4> shorts;
int* i = reinterpret_cast<int*>(shorts.data()); // Not OK

但是,该标准允许此规则的异常(exception):可以通过指向 char 的指针访问任何对象。或 unsigned char :
int i = 0;
char * c = reinterpret_cast<char*>(&i); // OK

但是,我不清楚这是否也允许反过来。例如:
char * c = read_socket(...);
unsigned * u = reinterpret_cast<unsigned*>(c); // huh?

最佳答案

由于涉及指针转换,您的某些代码存在问题。请记住,在这些情况下 reinterpret_cast<T*>(e)具有 static_cast<T*>(static_cast<void*>(e)) 的语义因为所涉及的类型是标准布局。 (实际上,我建议您在处理存储时始终通过 static_cast 使用 cv void*。)
仔细阅读标准表明,在指针转换为或来自 T* 期间假设确实存在一个实际对象 T*涉及——这在你的一些片段中很难实现,即使在“作弊”时也是如此,这要归功于所涉及的类型的琐碎(稍后会详细介绍)。然而,这不是重点,因为......
别名与指针转换无关。 这是 C++11 文本,概述了通常称为“严格别名”规则的规则,来自 3.10 Lvalues and rvalues [basic.lval]:

10 If a program attempts to access the stored value of an object through a glvalue 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 similar (as defined in 4.4) to 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 elements or non-static data members (including, recursively, an element or non-static data 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.

(这是 C++03 中相同条款和子条款的第 15 段,对文本进行了一些小的更改,例如使用“lvalue”而不是“glvalue”,因为后者是 C++11 概念。)
根据这些规则,让我们假设一个实现为我们提供了 magic_cast<T*>(p)其中“以某种方式”将指针转换为另一种指针类型。通常这将是 reinterpret_cast ,在某些情况下会产生未指定的结果,但正如我之前解释的那样,对于指向标准布局类型的指针,情况并非如此。那么很明显,您的所有片段都是正确的(用 reinterpret_cast 替换 magic_cast ),因为 magic_cast 的结果不涉及任何左值。 .
这是一个似乎错误地使用 magic_cast 的片段,但我认为这是正确的:
// assume constexpr max
constexpr auto alignment = max(alignof(int), alignof(short));
alignas(alignment) char c[sizeof(int)];
// I'm assuming here that the OP really meant to use &c and not c
// this is, however, inconsequential
auto p = magic_cast<int*>(&c);
*p = 42;
*magic_cast<short*>(p) = 42;
为了证明我的推理,假设这个表面上不同的片段:
// alignment same as before
alignas(alignment) char c[sizeof(int)];

auto p = magic_cast<int*>(&c);
// end lifetime of c
c.~decltype(c)();
// reuse storage to construct new int object
new (&c) int;

*p = 42;

auto q = magic_cast<short*>(p);
// end lifetime of int object
p->~decltype(0)();
// reuse storage again
new (p) short;

*q = 42;
这个片段是精心构建的。特别是在 new (&c) int;我可以使用 &c即使 c由于 3.8 对象生命周期 [basic.life] 的第 5 段中规定的规则而被销毁。第 6 段对存储引用给出了非常相似的规则,第 7 段解释了变量、指针和引用在重用存储后用于引用对象的情况——我将它们统称为 3.8/5- 7.
在这种情况下 &c被(隐式)转换为 void* ,这是对尚未重用的存储指针的正确使用之一。同样 p来自 &c新前 int被构造。它的定义或许可以在 c销毁后移到。 ,取决于实现魔法的深度,但肯定不是在 int 之后构造:第 7 段将适用,这不是允许的情况之一。 short的 build 对象也依赖于 p成为指向存储的指针。
现在,因为 intshort是微不足道的类型,我不必使用对析构函数的显式调用。我也不需要对构造函数的显式调用(也就是说,调用在 <new> 中声明的通常的标准位置 new )。从 3.8 对象生命周期 [basic.life]:

1 [...] The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • if the object has non-trivial initialization, its initialization is complete.

The lifetime of an object of type T ends when:

  • if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or
  • the storage which the object occupies is reused or released.

这意味着我可以重写代码,这样在折叠中间变量 q 之后,我最终得到了原始片段。
请注意 p不能折叠。也就是说,以下内容绝对是错误的:
alignas(alignment) char c[sizeof(int)];
*magic_cast<int*>(&c) = 42;
*magic_cast<short*>(&c) = 42;
如果我们假设 int对象是(简单地)用第二行构造的,那么这一定意味着 &c成为指向已重用存储的指针。因此第三行是不正确的——尽管由于 3.8/5-7 而不是由于严格意义上的别名规则。
如果我们不假设,那么第二行违反了别名规则:我们正在阅读实际上是 char c[sizeof(int)] 的内容。对象通过类型为 int 的泛左值,这不是允许的异常(exception)之一。相比之下, *magic_cast<unsigned char>(&c) = 42;会很好(我们假设一个 short 对象在第三行被简单地构造)。
就像 Alf 一样,我也建议您在使用存储时明确使用标准位置 new。跳过琐碎类型的破坏很好,但是遇到 *some_magic_pointer = foo; 时您很可能面临违反 3.8/5-7(无论该指针获得多么神奇)或别名规则的违反。这意味着也要存储 new 表达式的结果,因为一旦构造了对象,您很可能无法重用魔术指针——再次由于 3.8/5-7。
读取对象的字节(这意味着使用 charunsigned char )很好,但是您甚至不使用 reinterpret_cast或任何魔法。 static_cast通过 cv void*可以说很适合这项工作(尽管我确实觉得标准可以在那里使用一些更好的措辞)。

关于c++ - 允许使用 char* 别名 T*。反过来也允许吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12612488/

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