0

내 4D 동적 배열에서 요소에 액세스하려고하면 프로그램에서 "액세스 위반 읽기 위치"오류가 발생합니다. 여기 4D 동적 배열에서 액세스 위반 위치 읽기

내 할당을 Heres 코드

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, 실제로 벡터를 사용하지 않고 메모리 누락없이 절대 메모리를 찾을 수 없습니다.

나는 전체 코드를 읽을 수 없습니다,하지만 가장 먼저 :이 문제를 해결하기 때문에

+0

미로 때문에 4D 배열을 사용하는 것을 피할 수 없다. – nirajkumar

+0

나는 4D 배열을 사용하고있다. 최적화 목적으로 덩어리로 미로를 생성했기 때문에 심각한 로딩 시간없이 21^2보다 큰 미로를 만들 수 있습니다. – JustinWeq

+0

ok 예를 들어 [m * n]과 [m] [n]처럼 메모리를 할당 할 수있는 예를 들어 보겠습니다. 첫 번째 배열은 1D 배열이고 두 번째 배열은 2D 배열입니다. 더 혼란스럽고 더 많이 디버깅하기가 더 쉽습니다. 2D보다 ... – nirajkumar

답변

0

, 나는 (이하 "답"목록 조금 청소기를 얻기 위해) 대답 여기 내 댓글을 복사 할 수 있습니다 밖으로서는 것은 다음과 같습니다. int**** mazes에서 mazeGen으로 전달합니다.하지만 함수가 수행하는 첫 번째 작업은 전달 된 값을 무시하고 새 할당으로 바꾸는 것입니다.

호출자가 이러한 할당을 볼 수있게하려면 (내가 생각한대로, 메모리가 누출 됨) int**** &maze을 사용해야합니다. (그리고 나는 아직도 당신이 원시 포인터없이 더 나을 것이라고 생각하지만, 이것은 codereview.SE가 아니다.)