简体   繁体   English

c ++数组和内存错误

[英]Error with c++ Arrays and Memory

I have been working on converting a batch game I created to c++ for its further advancement, and have run into numerous problems, one of which is the array I need for the nodeMatrix (world map). 我一直在努力将自己创建的批处理游戏转换为c ++,以使其进一步发展,但遇到了许多问题,其中之一就是nodeMatrix(世界地图)所需的数组。 I need a 100 x 100 map with 20 data values per meter. 我需要一张100 x 100的地图,每米20个数据值。

 int nodeMatrix[99][99][19];

But the problem with it is when I try to set the null(or ungenerated) state of the map, it crashes with (0xC0000005), so I added a visual to the script that prints the current node being reset (it's much slower though), 但是它的问题是当我尝试设置地图的null(或未生成)状态时,它会崩溃,并显示为(0xC0000005),因此我在脚本中添加了一个视觉效果,用于打印正在重置的当前节点(不过速度要慢得多) ,

void emptydata(){

  int temp_x = 0;

  int temp_y = 0;

  int temp_t = 0;

  do{

     temp_y = 0;

     do{

         temp_t = 0;

         do{

             nodeMatrix[temp_x][temp_y][temp_t] = 0;

             //visual

             cout << temp_x << " " << temp_y << " " << temp_t << endl;

             temp_t ++;

         }while(temp_t <= 50);

         temp_y ++;

     }while(temp_y <= 99);

     temp_x ++;

 }while(temp_x <= 99);

}

It crashes at 99 14 10 each time, (it starts at zero so 100 15 11), would that be 16500 bites data? 每次它以99 14 10崩溃(它从零开始,所以100 15 11),那将是16500位数据吗?

Anyway, is it something with memory allocation? 无论如何,这与内存分配有关吗? I cant figure it out. 我不知道。

It sounds like your problem is that you have allocated a 99 by 99 by 19 array, rather than a 100 by 100 by 20 array. 听起来您的问题是您分配了99 x 99 x 19阵列,而不是100 x 100 x 20阵列。 Array declaration takes the number of elements, not the max index. 数组声明采用元素数量,而不是最大索引。

I'm not sure how you got to temp_y = 14 and temp_t = 10, but this seems to be confirmed by the fact that it crashes at temp_x = 99. 我不确定您如何达到temp_y = 14和temp_t = 10,但这似乎可以通过在temp_x = 99时崩溃来确认。

If you post that actual error message it would be more helpful. 如果您发布该实际错误消息,它将更有帮助。


It appears that you also limited temp_t by 50, rather than 20, but I think that was a typo. 看来您还将temp_t限制为50,而不是20,但是我认为这是一个错字。 Also, as a stylistic note, for loops are more common in c++, as they will handle indexing for you. 另外,作为风格注释,for循环在c ++中更为常见,因为它们将为您处理索引。

Your definition says [99][99][19] but you are checking for [100][100][51] (while(temp_t <= 50), while(temp_y <= 99), temp_x <= 99). 您的定义为[99] [99] [19],但您正在检查[100] [100] [51](while(temp_t <= 50),while(temp_y <= 99),temp_x <= 99)。

temp_t will go from 0 to 50 -> 51 elements. temp_t将从0变为50-> 51个元素。

temp_y will go from 0 to 99 -> 100 elements. temp_y将从0到99-> 100个元素。

temp_x will go from 0 to 99 -> 100 elements. temp_x将从0到99-> 100个元素。

When you get out of the reserved memory for the array you get a protected memory exception... 当您离开阵列的保留内存时,您会得到一个受保护的内存异常...

Also, for clarity, use for loops: 另外,为清楚起见,请使用for循环:

int nodeMatrix[100][100][20];

for(int x = 0; x < 100; x++)
{
    for(int y = 0; y < 100; y++)
    {
        for(int z = 0; z < 20; z++)
        {
            nodeMatrix[x][y][z] = 0;
        }
    }
}

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

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