简体   繁体   English

更改动态2D布尔数组中的一个值似乎会更改其他数组中不同位置的值

[英]Changing one value in a Dynamic 2D boolean array seemingly changes the value at a different place in a different array

I have two dynamically allocated boolean arrays of size height * width, that I allocated / initialized with... 我有两个动态分配的布尔数组,它们的大小为height * width,是我分配/初始化的...

bool ** visited; 
bool ** inQueue; 
visited = new bool * [height];
inQueue = new bool * [height];
for (int i = 0; i < height; i++) {
    visited[i] = new bool(width);
    inQueue[i] = new bool(width);
}

for (int i = 0; i < height; i++)
    for (int j = 0; j < width; j++) {
        visited[i][j] = false;
        inQueue[i][j] = false;
    }

And at the end of my program I clear up that memory with... 在程序结束时,我用...清除了内存

for (int i = 0; i < height; i++) {
    delete [] visited[i];
    delete [] inQueue[i];
}
delete [] visited;
delete [] inQueue;

But, in between, I get a weird condition where various values in visited's 0th column become true even though they are not being directly changed. 但是,在这两者之间,我遇到了一个奇怪的情况,即即使不直接更改它们,第0列中的各个值也变为真实。

An example of this behavior is: 此行为的一个示例是:

. . . . . . . . . . . X . . . . .       0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 
    . . . . . . # . # . X X X . # . .       0 0 0 0 0 0 1 0 1 0 1 1 1 0 1 0 0 
    . . . . . . # . . # X X X X # . .       0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 0 0 
    . . . . . . # . X X X X X X # . .       0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 0 0 
    . . . . . . # # # # # X X X # . .       0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 
    . # . . . . . . . . # X X X X @ .       0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 
    . # . . . . . . . . # X X X X . .       1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 
    . # . . . . . . . . # X X X . . .       0 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 
    . G # . . . # # # . # X X . # . .       0 0 1 0 0 0 1 1 1 0 1 1 1 0 1 0 0 
    . # . . . . . . . . . X . . . . .       0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 
    . . . . . . . . . . # . . . . . .       0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 
    . . . . . . . . . . . . . . . . .       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    . . . . . . . . . . . . . . . . .       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    . . . . . . . . . . . . . . . . .       0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
    . . . . . . # . . . . . . . # . .       0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 
    . . . . . . # . . . . . . . # . .       0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 
    . . . . . . # . . . . . . . # . .       0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 

    [6][0] @ 205: 0
    Added Eastern Pos: (16, 5)
    [6][0] @ 208: 1
    [6][0] @ 210: 1
    ...

This is my output. 这是我的输出。 As you can see, the position in visited[6][0] becomes true on what I've labeled in my output after line 205, while adding the eastern position. 如您所见,在第205行之后,在我已在输出中标记的位置上,visited [6] [0]中的位置变为真实,同时添加了东部位置。 That code for that change is here: 该更改的代码在这里:

cout << "[6][0] @ 205: " << visited[6][0] << endl;
cout << "Added Eastern Pos: (" << (east % width) << ", " << (int)floor(east / width) << ")\n";
inQueue[(int)floor(east / width)][east % width] = true;
cout << "[6][0] @ 208: " << visited[6][0] << endl;
xVal.addToQueue(to_string(east % width));

Specifically on the third line I change the value of inQueue, and then immediately afterwards (as seen in the block above) visited[6][0] has changed to true. 具体地说,在第三行,我更改了inQueue的值,然后在访问后[6] [0]变为true之后(如在上面的块中所示)。 Keep in mind that 请记住

(int)floor(east / width) (int)楼层(东/宽度)

is actually 5, and 实际上是5,

east % width 东%宽度

is 16. So, to me it makes no sense whatsoever that visited would be changed. 是16。所以,对我而言,访问没有任何改变。 This happens incrementally throughout my programs run, always with values of the 0th column being changed. 在我的程序运行过程中,这是递增发生的,始终会更改第0列的值。

Any ideas? 有任何想法吗?

With visited[i] = new bool(width) , you allocate a single boolean value, not an array of booleans. 使用visited[i] = new bool(width) ,您将分配一个布尔值,而不是布尔数组。 Henceforth, when expecting an array of width elements, which actually consists only of one, you exceed array bounds and get undefined behaviour (and that's what you see in your output). 此后,当期望一个width元素数组(实际上仅由一个元素组成)时,您将超出数组范围并得到未定义的行为(这就是您在输出中看到的)。

See the difference: 看到不同:

int main() {
    int width =20;
    int sizeofBool = sizeof(bool(width)); // -> 1
    int sizeOfBoolArray = sizeof(bool[width]); // -> 20
}

So write visited[i] = new bool[width] (and also for inQueue ), and you should at least get ahead. 因此,写inQueue visited[i] = new bool[width] (也用于inQueue ),您至少应该取得成功。

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

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