在c ++的二维数组中为什么我们总是要提到列数?

时间:2021-07-02 21:36:38

this is a very simple question but whoever i ask it they cant answer it. in two dimensional arrays we have to specify rows and columns,right? for example:

这是一个非常简单的问题,但无论我问谁,他们都无法回答。在二维数组中,我们必须指定行和列,对吧?例如:

void foo( int towers **[ ][ 3 ]** , int rings )
{
  for (int ring = 0; ring < rings; ring++)
  {
    ... towers[ ring ][ 0 ]...
  }
}

in programming its okay if we dont write(mention) rows as you see in that piece of code,but we ALWYAS have to write number of columns no matter what. so what is the logic reason that why we should write number of columns? what will happen if we dont?

如果我们不在那段代码中看到(提及)行,那么在编程中它没关系,但我们ALWYAS必须写入列数,无论如何。那么为什么我们应该写出列数的逻辑原因是什么?如果我们不这样做会发生什么?

2 个解决方案

#1


3  

Whenever you try to access a two dimensional array, you specify the two indices: array[a][b]. C++ ensures all the elements in array[n][...] are contiguous in memory for any n, then the next bit of memory is used for array[n+1][...] etc.. For example:

每当您尝试访问二维数组时,都要指定两个索引:array [a] [b]。 C ++确保array [n] [...]中的所有元素在任何n的内存中是连续的,然后下一位内存用于数组[n + 1] [...]等。例如:

               array[0][0]  ((T*)array)
               array[0][1]  ((T*)array) + 1
               array[0][2]  ((T*)array) + 2
               array[1][0]  ((T*)array) + 3
               array[1][1]  ((T*)array) + 4
               ...

So, the absolute memory address of array[a][b], in an array T array[A][B] is:

因此,数组[a] [b]的绝对存储器地址在数组T array [A] [B]中是:

((T*)array) + a * B + b;

See how the calculation needs B but not A? Similarly, the compiler is insisting you provide all but the left-most index.

看看计算如何需要B而不是A?同样,编译器坚持要提供除最左边索引之外的所有索引。

In some ways, it would be nice if the compiler verified that you didn't try to index to a value of [a] at or past A, but the language makes no such checks - it's up to the programmer to ensure their code indexes the array safely. If you want safety you can use a std::vector<> and at() for runtime index checking, or even write a fixed-size-array mechanism that checks compile-time-constant indices at compile time (not very useful, as indices typically vary at runtime).

在某些方面,如果编译器验证您没有尝试在A处或之后索引[a]的值,但该语言不进行此类检查,那将是很好的 - 由程序员确保其代码索引阵列安全。如果你想要安全,可以使用std :: vector <>和at()进行运行时索引检查,甚至编写一个固定大小的数组机制,在编译时检查编译时常量索引(不是很有用,如索引通常在运行时变化)。

#2


1  

You know how two dimensional array is stored in the memory. If it is of size [n][m] then it just takes n x m consecutive cells in the memory. Afterwords when you get asked about element [a][b] there is the calculation that takes the element a * m + b from these consecutive cells. However this calculation can not be performed if you do not supply the number of columns (because otherwise the m will not be known).

你知道二维数组是如何存储在内存中的。如果它的大小是[n] [m]那么它只需要在存储器中连续n×m个单元。当你被问到关于元素[a] [b]的后面时,有一个计算从这些连续的单元格中取出元素a * m + b。但是,如果不提供列数,则无法执行此计算(因为否则m将不知道)。

On the other hand the number of columns and rows is not stored in the memory portion for the array and the number of columns can not be derived from the memory either. All this means that the computer will not know which memory address to serve you.

另一方面,列和行的数量不存储在阵列的存储器部分中,并且也不能从存储器导出列数。所有这些意味着计算机将不知道为您提供哪个内存地址。

#1


3  

Whenever you try to access a two dimensional array, you specify the two indices: array[a][b]. C++ ensures all the elements in array[n][...] are contiguous in memory for any n, then the next bit of memory is used for array[n+1][...] etc.. For example:

每当您尝试访问二维数组时,都要指定两个索引:array [a] [b]。 C ++确保array [n] [...]中的所有元素在任何n的内存中是连续的,然后下一位内存用于数组[n + 1] [...]等。例如:

               array[0][0]  ((T*)array)
               array[0][1]  ((T*)array) + 1
               array[0][2]  ((T*)array) + 2
               array[1][0]  ((T*)array) + 3
               array[1][1]  ((T*)array) + 4
               ...

So, the absolute memory address of array[a][b], in an array T array[A][B] is:

因此,数组[a] [b]的绝对存储器地址在数组T array [A] [B]中是:

((T*)array) + a * B + b;

See how the calculation needs B but not A? Similarly, the compiler is insisting you provide all but the left-most index.

看看计算如何需要B而不是A?同样,编译器坚持要提供除最左边索引之外的所有索引。

In some ways, it would be nice if the compiler verified that you didn't try to index to a value of [a] at or past A, but the language makes no such checks - it's up to the programmer to ensure their code indexes the array safely. If you want safety you can use a std::vector<> and at() for runtime index checking, or even write a fixed-size-array mechanism that checks compile-time-constant indices at compile time (not very useful, as indices typically vary at runtime).

在某些方面,如果编译器验证您没有尝试在A处或之后索引[a]的值,但该语言不进行此类检查,那将是很好的 - 由程序员确保其代码索引阵列安全。如果你想要安全,可以使用std :: vector <>和at()进行运行时索引检查,甚至编写一个固定大小的数组机制,在编译时检查编译时常量索引(不是很有用,如索引通常在运行时变化)。

#2


1  

You know how two dimensional array is stored in the memory. If it is of size [n][m] then it just takes n x m consecutive cells in the memory. Afterwords when you get asked about element [a][b] there is the calculation that takes the element a * m + b from these consecutive cells. However this calculation can not be performed if you do not supply the number of columns (because otherwise the m will not be known).

你知道二维数组是如何存储在内存中的。如果它的大小是[n] [m]那么它只需要在存储器中连续n×m个单元。当你被问到关于元素[a] [b]的后面时,有一个计算从这些连续的单元格中取出元素a * m + b。但是,如果不提供列数,则无法执行此计算(因为否则m将不知道)。

On the other hand the number of columns and rows is not stored in the memory portion for the array and the number of columns can not be derived from the memory either. All this means that the computer will not know which memory address to serve you.

另一方面,列和行的数量不存储在阵列的存储器部分中,并且也不能从存储器导出列数。所有这些意味着计算机将不知道为您提供哪个内存地址。