简体   繁体   English

C ++,为什么多维数组中一个元素的增加似乎正在增加另一个元素?

[英]C++, why is an increase in one element of a multi-dimensional array appear to be increasing another?

This may not be elegant. 这可能并不优雅。 Chiefly because I am relatively new to C++, but this little program I am putting together is stumbling here. 主要是因为我对C ++比较陌生,但是我编写的这个小程序却在这里绊脚石。

I don't get it. 我不明白 Have I misunderstood arrays? 我是否误解了数组? The edited code is: 编辑后的代码为:

int diceArray [6][3][1] = {};

...

}else if (y >= xSuccess || x >= xSuccess){

// from here...

    diceArray[2][1][0] = diceArray[2][1][0] + 1;
    diceArray[2][1][1] = diceArray[2][1][1] + 1;

// ...to here, diceArray[2][2][0] increases by 1. I am not referencing that part of the array at all. Or am I?

}

By using comments I tracked the culprit down to the second expression. 通过使用注释,我将罪魁祸首追溯到第二种表达方式。 If I comment out the first one diceArray[2][2][0] does not change. 如果我注释掉第一个,则diceArray[2][2][0]不会改变。

Why is diceArray[2][1][1] = diceArray[2][1][1] + 1 causing diceArray[2][2][0] to increment? 为什么diceArray[2][1][1] = diceArray[2][1][1] + 1导致diceArray[2][2][0]增加?

I tried.. 我试过了..

c = diceArray[2][1][1] + 1;

diceArray[2][1][1] = c;

..as a workaround but it was just the same. ..作为一种解决方法,但还是一样。 It increased diceArray[2][2][0] by one. 它使diceArray[2][2][0]增加了一。

You are indexing out of bounds. 您索引超出范围。 If I declare such an array 如果我声明这样的数组

int data [3];

Then the valid indices are 那么有效的索引是

data[0]
data[1]
data[2]

The analog to this is that you declare 与此类似,您声明

int diceArray [6][3][1]
                     ^

But then try to assign to 但是然后尝试分配给

diceArray[2][1][0]
                ^

diceArray[2][1][1]     // This is out of range
                ^

Since you are assigning out of range, due to pointer arithmetic you are actually assigning to the next dimension due to striding, etc. 由于您分配的范围超出范围,由于指针算法的原因,您实际上是由于跨度等而分配给下一维。

The variable is declared as: 该变量声明为:

int diceArray [6][3][1] = {};

This is how it looks like in memory: 这是在内存中的样子:

+---+                       -.
|   |  <- diceArray[0][0]     \
+---+                          \
|   |  <- diceArray[0][1]       > diceArray[0]
+---+                          /
|   |  <- diceArray[0][2]     /
+---+                       -'
|   |  <- diceArray[1][0]     \
+---+                          \
|   |  <- diceArray[1][1]       > diceArray[1]
+---+                          /
|   |  <- diceArray[1][2]     /
+---+                       -'
  .            .                      .
  .            .                      .
  .            .                      .
+---+                       -.
|   |  <- diceArray[5][0]     \
+---+                          \
|   |  <- diceArray[5][1]       > diceArray[5]
+---+                          /
|   |  <- diceArray[5][2]     /
+---+                       -'

The innermost component of diceArray is an array of size 1 . diceArray的最内部组件是大小为1的数组。 C / C++ arrays are always indexed starting from 0 and that means the only valid index in and array of size 1 is 0 . C / C++数组始终从0开始索引,这意味着in和大小为1数组中唯一有效的索引为0

During the compilation, a reference to diceArray[x][y][z] is converted using pointer arithmetic to offset x*3*1+y*1+z ( int values) using the memory address of diceArray as base. 在编译,到参考diceArray[x][y][z]使用指针运算,以抵消被转换x*3*1+y*1+zint使用的存储器地址的值) diceArray作为碱。

The code: 编码:

diceArray[2][1][1] = diceArray[2][1][1] + 1;

operates on offset 8 (=2*3*1+1*1+1) inside diceArray . diceArray内部的偏移量8 (=2*3*1+1*1+1)进行diceArray The same offset is computed using diceArray[2][2][0] , which is a legal access inside the array. 使用diceArray[2][2][0]计算相同的偏移量,这是数组内部的合法访问。

The modern compilers are usually able to detect this kind of errors and warn you on the compilation. 现代的编译器通常能够检测到此类错误并向您发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM