c++ - 将一维数组 reshape 为多维数组

标签 c++ c++11 multidimensional-array language-lawyer

考虑到整个 C++11 标准,任何符合要求的实现是否有可能成功下面的第一个断言但失败了后者?

#include <cassert>

int main(int, char**)
{  
    const int I = 5, J = 4, K = 3;
    const int N = I * J * K;

    int arr1d[N] = {0};
    int (&arr3d)[I][J][K] = reinterpret_cast<int (&)[I][J][K]>(arr1d);
    assert(static_cast<void*>(arr1d) ==
           static_cast<void*>(arr3d)); // is this necessary?

    arr3d[3][2][1] = 1;
    assert(arr1d[3 * (J * K) + 2 * K + 1] == 1); // UB?
}

如果不是,这在技术上是否是 UB,如果第一个断言被删除,答案是否会改变(reinterpret_cast 是否保证在这里保留地址?)?另外,如果 reshape 是在相反的方向(3d 到 1d)或从 6x35 阵列到 10x21 阵列进行的?

编辑:如果答案是由于 reinterpret_cast 而导致这是 UB,是否还有其他一些严格兼容的 reshape 方式(例如,通过 static_cast 到/从中间 void *)?

最佳答案

2021-03-20 更新:

同样的问题是 asked on Reddit最近有人指出我的原始答案有缺陷,因为它没有考虑到这个 aliasing rule :

If a program attempts to access the stored value of an object through a glvalue whose type is not similar to one of the following types the behavior is undefined:

  • the dynamic type of the object,
  • a type that is the signed or unsigned type corresponding to the dynamic type of the object, or
  • a char, unsigned char, or std​::​byte type.

根据 similarity 的规则,这两种数组类型在上述任何情况下都不相似,因此通过 3D 数组访问 1D 数组在技术上是未定义的行为。 (这绝对是其中一种情况,在实践中,它几乎可以肯定适用于大多数编译器/目标)

请注意,原始答案中的引用是指较旧的 C++11 草案标准

原答案:

reinterpret_cast引用文献

标准规定 T1 类型的左值可以是reinterpret_castT2 的引用如果指向 T1 的指针可以是reinterpret_cast指向 T2 的指针(§5.2.10/11):

An lvalue expression of type T1 can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast.

所以我们需要确定 int(*)[N]可以转换为 int(*)[I][J][K] .

reinterpret_cast指针

指向 T1 的指针可以是reinterpret_cast指向 T2 的指针如果两者都是 T1T2是标准布局类型和 T2没有比 T1 更严格的对齐要求(§5.2.10/7):

When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cv T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment requirements of T2 are no stricter than those of T1, or if either type is void.

  1. int[N]int[I][J][K]标准布局类型?

    int是标量类型,标量类型的数组被视为标准布局类型(第 3.9/9 节)。

    Scalar types, standard-layout class types (Clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called standard-layout types.

  2. 是否 int[I][J][K]没有比 int[N] 更严格的对齐要求.

    alignof 的结果运算符给出完整对象类型的对齐要求(第 3.11/2 节)。

    The result of the alignof operator reflects the alignment requirement of the type in the complete-object case.

    由于这里的两个数组不是任何其他对象的子对象,它们是完整的对象。申请alignof到数组给出元素类型的对齐要求(§5.3.6/3):

    When alignof is applied to an array type, the result shall be the alignment of the element type.

    所以这两种数组类型具有相同的对齐要求。

这使得 reinterpret_cast有效且等价于:

int (&arr3d)[I][J][K] = *reinterpret_cast<int (*)[I][J][K]>(&arr1d);

在哪里 *&是内置运算符,则相当于:

int (&arr3d)[I][J][K] = *static_cast<int (*)[I][J][K]>(static_cast<void*>(&arr1d));

static_cast通过void*

static_castvoid*标准转换(§4.10/2)允许:

A prvalue of type “pointer to cv T,” where T is an object type, can be converted to a prvalue of type “pointer to cv void”. The result of converting a “pointer to cv T” to a “pointer to cv void” points to the start of the storage location where the object of type T resides, as if the object is a most derived object (1.8) of type T (that is, not a base class subobject).

static_castint(*)[I][J][K]然后被允许(§5.2.9/13):

A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T,” where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.

所以转换没问题!但是我们可以通过新的数组引用访问对象吗?

访问数组元素

对像 arr3d[E2] 这样的数组执行数组下标相当于 *((E1)+(E2)) (§5.2.1/1)。让我们考虑以下数组下标:

arr3d[3][2][1]

首先,arr3d[3]相当于 *((arr3d)+(3)) .左值 arr3d进行数组到指针的转换,得到 int(*)[2][1] .不要求底层数组必须是正确的类型才能进行此转换。然后访问指针值(第 3.10 节很好),然后将值 3 添加到它。这种指针算法也很好(§5.7/5):

If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.

这个 this 指针被取消引用以给出 int[2][1] .这对接下来的两个下标进行相同的过程,产生最终的 int在适当的数组索引处的左值。由于 * 的结果,它是一个左值(§5.3.1/1):

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.

这样就可以访问实际的 int对象通过这个左值,因为左值的类型是 int太(§3.10/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
  • [...]

所以除非我错过了什么。我会说这个程序定义明确。

关于c++ - 将一维数组 reshape 为多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283523/

相关文章:

php - 如何创建键名与分配给数组的 $ 变量名相同的数组?

javascript - 在 Javascript 中对整数数组执行计算

python - 如何向量化多维 numpy 数组中的提取

c++ - Stroustrup : C++ implementation of reserve()

c++ - static_assert 和类模板

C++ wstring 如何从 NULL 终止的 wchar_t 数组分配

c++ - std::any 由 std::exception_ptr

c++ - 使用用户定义的文字有条件地包含在 C++11 中?

c++ - 如果我调用 new 和 delete,管理 dll 类的内存是否安全?

c++ - 尝试编译 C 扩展模块时缺少 Python.h