0

尝试访问 4D 动态数组中的元素时,我的程序中出现“访问冲突读取位置”错误。

这是我的分配代码

void mazeGen(int**** mazes, int width, int height,stack<SDL_Point> backtracker)
{
    numberOfCalls++;
    //first choose a starting location for the maze
    //starting location must be odd in order to ensure that the generator does not go outside
    //the maze bounds.

    //allocate memory for the mazes
    mazes = new int***[NUMHOR];
    for (int i = 0; i < NUMVER; i++)
    {
        mazes[i] = new int**[NUMVER];
    }
    //allocate memory for the actual mazes
    for (int x = 0; x < NUMHOR; x++)
    {
        for (int y = 0; y < NUMVER; y++)
        {
            mazes[x][y] = initMaze(WIDTH, HEIGHT);
        }
    }
    //mazeGenHelper(maze, height, width, backtracker, start);
    bool leftToRight = true;
    for (int x = 0; x < NUMHOR; x++)
    {
        for (int y = 0; y < NUMVER; y++)
        {
            //generate mazes
            SDL_Point* start = new SDL_Point();
            //genx
            do
            {
                start->x = generateRandomRange(1, width - 1);
            } while (start->x % 2 == 0);
            //gen y
            do
            {
                start->y = generateRandomRange(1, height - 1);
            } while (start->y % 2 == 0);
            //empty stack
            while (!backtracker.empty())
            {
                backtracker.pop();
            }
            mazeGenHelper(mazes[x][y], HEIGHT, WIDTH, backtracker, start);
            //delete start to prevent memory leak
            delete start;
        }
    }
}

这是其余的(它是一个迷宫生成程序,以防你无法分辨)

void mazeGenHelper(int** maze, int height, int width, stack<SDL_Point> backtracker, SDL_Point* point,SDL_Point* endPoint)
{
    numberOfCalls++;
    array<int, 4> directions = shuffleDirections();
    for (int i = 0; i < 4; i++)
    {
        switch (directions[i])
        {
        case 1://up
        {
                  if (point->y - 2 > 0 && maze[point->x][point->y - 2] == 1)
                  {
                      //delete maze walls
                      maze[point->x][point->y - 1] = 0;
                      maze[point->x][point->y - 2] = 0;
                      //add current point to the backtracker
                      SDL_Point newPoint = { point->x, point->y };
                      backtracker.push(newPoint);
                      //move the current point
                      point->y -= 2;
                      mazeGenHelper(maze, height, width, backtracker, point,endPoint);
                  }
        }
        case 2://right
        {
                   if (point->x + 2 <width && maze[point->x+2][point->y] == 1)
                   {
                       //delete maze walls
                       maze[point->x+1][point->y] = 0;
                       maze[point->x+2][point->y] = 0;
                       //add current point to the backtracker
                       SDL_Point newPoint = { point->x, point->y };
                       backtracker.push(newPoint);
                       //move the current point
                       point->x += 2;
                       mazeGenHelper(maze, height, width, backtracker, point,endPoint);
                   }
        }
        case 3://down
        {
                   if (point->y + 2 < height && maze[point->x][point->y + 2] == 1)
                   {
                       //delete maze walls
                       maze[point->x][point->y + 1] = 0;
                       maze[point->x][point->y + 2] = 0;
                       //add current point to the backtracker
                       SDL_Point newPoint = { point->x, point->y };
                       backtracker.push(newPoint);
                       //move the current point
                       point->y += 2;
                       mazeGenHelper(maze, height, width, backtracker, point,endPoint);
                   }
        }
        case 4://left
        {
                  if (point->x - 2 > 0 && maze[point->x - 2][point->y] == 1)
                  {
                      //delete maze walls
                      maze[point->x - 1][point->y] = 0;
                      maze[point->x - 2][point->y] = 0;
                      //add current point to the backtracker
                      SDL_Point newPoint = { point->x, point->y };
                      backtracker.push(newPoint);
                      //move the current point
                      point->x -= 2;
                      mazeGenHelper(maze, height, width, backtracker, point,endPoint);
                  }
        }
        }
    }
    if (backtracker.size() != 0)
    {
        //pop curent element off the stack and recall
        SDL_Point newPoint = backtracker.top();
        endPoint->x = newPoint.x;
        endPoint->x = newPoint.y;
        backtracker.pop();
        mazeGenHelper(maze, height, width, backtracker, &newPoint,endPoint);
    }
    // else the maze must be done
}

这是我试图访问它

void sdlapp::render()
{
    //clear the screen
    SDL_RenderClear(m_renderer);
    //do render stuff here

    //rect area for 
    SDL_Rect rect = { 0,0, zoomLevel, zoomLevel };

    //render the maze walls
    for (int i = 0; i < WIDTH;i++)
    for (int k = 0; k < HEIGHT; k++)
    {
        switch (maze[i][k])//<- thats where i am trying to access it.
        {
        case 1: // theres a wall
        {
                    rect.x = i * zoomLevel-camera->x;
                    rect.y = k * zoomLevel-camera->y;
                    SDL_RenderCopy(m_renderer, mazeWallTex, NULL, &rect);
        }
            break;
        case 2: //theres a start point
        {
                    rect.x = i * zoomLevel - camera->x;
                    rect.y = k * zoomLevel - camera->y;
                    SDL_RenderCopy(m_renderer, mazeStartTex, NULL, &rect);
        }
        case 3:
        {
                  rect.x = i * zoomLevel - camera->x;
                  rect.y = k * zoomLevel - camera->y;
                  SDL_RenderCopy(m_renderer, mazeEndTex, NULL, &rect);
        }
        }

    }
    //update the screen to the current render
    SDL_RenderPresent(m_renderer);
}

我不希望您通读所有这些代码,但无论如何我都发布了。如果有人知道我做错了什么,你能指出我正确的方向吗?

感谢您抽出时间 JustinWeq,顺便说一句,我真的不想使用向量,并且可以正确处理定位我的内存,绝对没有内存泄漏。

4

1 回答 1

0

由于它解决了问题,我将在此处复制我的评论作为答案(以使“未回答”列表更清晰一些):

我还没有阅读完整的代码,但最突出的是:你传入一个int**** mazesto mazeGen,但该函数做的第一件事就是丢弃传入的值并用新的分配替换它。

如果您希望调用者可以看到这些分配(我假设您这样做;就目前而言,该内存只是泄漏),您需要使用int**** &maze. (而且我仍然认为没有原始指针会更好,但这不是 codereview.SE。)

于 2015-01-08T06:57:00.080 回答