为什么需要多维数组中更高维度的维度范围? [英] Why is dimension range of higher dimensions in multi-dimentional array required?

查看:118
本文介绍了为什么需要多维数组中更高维度的维度范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据帖子,

将2D数组传递给C ++函数

int array[10][10];
void passFunc(int a[][10]) //  <---Notice 10 here
{
    // ...
}
passFunc(array);

为什么编译器内部需要这个更高的维度。

Why is this higher dimension required from compilers internal point of view.

推荐答案

另一个解释(数组到指针衰减):

An alternative explanation (to array-to-pointer decay):

一维数组,我们这样使用它:

Let's say we have a one-dimensional array, and we use it like this:

int array[10];
int i = array[3];

编译器必须知道在哪里找到 array [3] 。它知道它需要跳过3 int s,然后才能访问 array [3] 中的那个。

The compiler has to know where to find array[3]. It knows it needs to skip 3 ints before it can get to the one in array[3]. So it works.

但是如果我们有一个二维数组,

But if we have a two-dimensional array,

int array[2][5];
int i = array[1][1];

要在此处获取 i code> int s编译器需要跳过吗?它需要跳过一整行,加一。跳过一个很容易,因为我们知道一个 int 的大小。但是我们还需要知道数组中行的大小,并且行的大小由类型的大小*每行的列数决定。这是一种看待它的方式,这就解释了为什么你需要后一个维度。

To get i here, how many ints does the compiler need to skip? It needs to skip an entire row, plus one. To skip one is easy, since we know the size of one int. But we also need to know the size of the row in the array—and the size of the row is determined by the size of the type * number of columns per row. This is one way of looking at it, which explains why you need the latter dimension.

让我们做一个小的脑筋急转弯, p>

Let's make this a small brain teaser by taking it one dimension further, to

int array[2][2][2];
int i = array[1][1][1];

,我们调用尺寸 X,Y,Z

在这里,我们可以说我们有一个有限的三维空间 int s。单位当然是一个 int 的大小。行数由 Y 定义,平面数由 Z 定义。这留下了 X 作为基本单位,这是一个 int 的大小,正如我们所说。三个组合产生一个点。

Here, we can say we have a finite 3D space of ints. The unit is of course the size of one int. The number of rows is defined by Y, the number of planes is defined by Z. That leaves X as the basic unit, which is the size of one int, as we said. The combination of the three yields a "point."

为了能够到达3D空间中的任何点,我们需要知道每个维度停止下一个开始。所以我们需要:

To be able to get to any point in that 3D space, we need to know where each dimension "stops" and the next one begins. So we need:


  1. 单元的大小( int ) 维度

  2. 每个平面的大小( Y / li>
  3. 穿过 Z 维度的平面数量

  1. The size of the unit (int), to traverse the X dimension
  2. The size of each plane (Y), to traverse the Y dimension
  3. The number of planes, to traverse the Z dimension

再次, X 已经提供给我们,因为我们使用 int 。但我们不知道每架飞机的大小,我们也不知道有多少飞机。因此,我们需要指定除第一个维度之外的所有值。这是一般规则。

So again, X is already given to us, because we're using int. But we don't know the size of each plane, nor do we know how many planes there are. So we need to specify all but the first dimension. And that's the general rule.

这也解释了为什么这个问题比单纯的指针衰减更加详细的解释,因为一旦你得到超过2维,你仍然需要知道这是如何工作的。
换句话说,您需要整体大小(维度的乘积)不会溢出,并且您需要每个大小的维度才能使用连续的 [ ] 索引。

This also explains why this issue invites a bit more elaborate explanation than mere pointer decay, because once you get to more than 2 dimensions, you still need to know how this works. In other words, you need the overall size (product of dimensions) to not overflow, and you need the dimension of each size to be able to use successive [] indices.

这篇关于为什么需要多维数组中更高维度的维度范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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