这个C ++代码是可移植的吗? (假设多维数组具有连续的内存布局) [英] Is this C++ code portable? (assuming multidimensional arrays have continuous memory layout)

查看:166
本文介绍了这个C ++代码是可移植的吗? (假设多维数组具有连续的内存布局)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于我的英语,如果我犯任何语法错误等等...



我的问题是,当我们有一个二维数组,那么如果我是对的,从计算机和C / C ++的角度来看,它只是一个长一维数组,索引只是帮助编译器映射到具体的地址。



此代码片段在Visual C ++中有效,但我想知道,此代码是可移植符合 (C ++ 98),不会对其他架构和/或操作系统造成意外:

  int arr [] [3] = {1,5,3,7,5,2,7,8,9}; 
const int ARR_NUM = sizeof(arr)/ sizeof(int);

int * ptr = reinterpret_cast< int *>(arr); // NOT:int(*)[] [3] !!!
for(int i = 0; i cout< ptr [i]< endl
}


解决方案

b
$ b

多维数组的元素以行为主的顺序顺序存储,因此手动索引是可移植的:



C ++ 98,8.3.4 / 1:


数组类型的对象包含一个连续分配的非空的
- 类型为T的对象。


显然,对于多维数组,这是递归应用。



但是, reinterpret_cast 的使用不可移植。标准说(C ++ 98,5.2.10 / 1)


[...]否则结果是右值[...],
数组到指针,[...]对
表达式执行标准转换。


换句话说,传递 arr 会立即触发数组衰减到指向其第一个元素的指针。然后(C ++ 98,5.2.10 / 3)来自catch-all



本节的其余部分列出了大量的异常,指定了铸造,总是很好定义。



最终结论



p>理论上讲,这不是便携式的。实际上,只要架构是相同的(例如x86),我一定会期望转换可靠地工作。



幸运的是,你不必假设因为其他人 已经提到过,例如 int * ptr = arr [0] 做同样的事情,并保证便携。


First of all, sorry for my english if i make any grammar mistakes, etc ...

My question is, that when we have a two dimensional array, then if i am right, from the point of the computer and C/C++, it's just a long one dimensional array, the indices are just helping the compiler to map to the concrete address.

This code fragment works in Visual C++, however I would like to know, whether this code is portable and conforms to the standard (C++98), not causing surprises on other architectures and/or operating systems:

int arr[][3] = { 1, 5, 3, 7, 5, 2, 7, 8, 9 };
const int ARR_NUM = sizeof(arr) / sizeof(int);

int* ptr = reinterpret_cast<int*>(arr);    // NOT: int(*)[][3] !!!
for (int i = 0; i < ARR_NUM; ++i) {
    cout << ptr[i] << endl;
}

解决方案

Standardese

The elements of the multidimensional array are stored sequentially in row-major order, so the manual indexing is portable:

C++98, 8.3.4/1:

An object of array type contains a contiguously allocated non-empty set of N sub-objects of type T.

Obviously for a multidimensional array this applies recursively.

However, this use of reinterpret_cast is not portable. The standard says (C++98, 5.2.10/1) that

[...] otherwise, the result is an rvalue and the [...], array-to-pointer, [...] standard conversions are performed on the expression v.

In other words, passing arr immediately triggers a decay of the array to a pointer to its first element. Then (C++98, 5.2.10/3) comes the catch-all

The mapping performed by reinterpret_cast is implementation-defined.

The rest of the section lists a number of exceptions to this, specifying casts that are always well-defined. Seeing as none of them applies here, the conclusion is that technically it's implementation-defined by default.

Final conclusion

Theoretically speaking, this is not portable. Practically, as long as the architectures are the same (e.g. x86) I would certainly expect the cast to work reliably.

Fortunately you don't have to assume anything like that because as others have mentioned, something like int* ptr = arr[0] does the same thing and is guaranteed portable.

这篇关于这个C ++代码是可移植的吗? (假设多维数组具有连续的内存布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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