简体   繁体   English

访问2D数组时读取违规

[英]Read Violation when Accessing 2D array

I got a really weird read violation error when I try to access the addresses that are supposed to be inside a 2D array. 当我尝试访问应该在2D数组中的地址时,出现了一个非常奇怪的读取冲突错误。 Please read several lines of my code: 请阅读我的代码的几行:

Class class : public SuperClass
{ 
public:
    bool checkDirt(int x, int y)
    {
        if(DirtField[x][y] != nullptr) //read violation error given here
................

private:
    Dirt* DirtField[64][60];
}

The DirtField 2D array consist of pointers to Dirt objects and nullptrs DirtField 2D数组由指向Dirt对象和nullptrs的指针组成

When I tried to debug the program, it tells me the x and y are always well with in the bound of the 2D array, say x=21, y=14. 当我尝试调试程序时,它告诉我x和y在2D数组的边界内总是很好,例如x = 21,y = 14。 But no matter what value is x and y I always get the error. 但是无论x和y是什么值,我总是会收到错误。

Please help. 请帮忙。 Thanks a lot! 非常感谢!

Let's suppose that the debugger gives the correct place of the access violation and let's proceed by elimination: 让我们假设调试器给出了访问冲突的正确位置,让我们通过消除进行:

  • DirtField is a fixed 64x60 array. DirtField是固定的64x60数组。 So DirtField can't be nullptr, and the read access error can't come from allocation of DirtField . 因此DirtField不能为nullptr,并且读取访问错误也不能来自DirtField分配。
  • if you have checked that x and y are both within range, the read access error can't come from an out of bound access to the array either. 如果您已检查xy都在范围内,则读取访问错误也不可能来自对数组的无限制访问。
  • DirtFiled[x][y] contains a pointer to Dirt that you compare with anothers pointer without derefencing any of them. DirtFiled[x][y]包含一个指向Dirt的指针,您可以将其与另一个指针进行比较,而不会取消引用它们中的任何一个。 So it couldn't be an issue with the Dirt class either, or the content of the DirtField array. 因此, Dirt类或DirtField数组的内容都不是问题。

The only problem left, would be that the object on which you've called checkDirt() is itself invalid. 剩下的唯一问题是,您在其上调用checkDirt()对象本身是无效的。 Something like: 就像是:

Class *myobject;  // unitinialized pointer 
myobject->checkDirt(21,14);  

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

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