我收到了一个"访问违规阅读位置"尝试访问时我的程序出错 我的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, 顺便说一句,我真的不想使用向量,并且可以正确地处理我的内存,绝对没有内存泄漏。
答案 0 :(得分:0)
既然它解决了这个问题,我会在这里复制我的评论作为答案(让“未答复”的清单更清晰一点):
我还没有阅读整个代码,但首先要突出的是:
您将int**** mazes
传递给mazeGen
,但该函数的第一件事就是丢弃传入的值并用新分配替换它。
如果您希望调用者可以看到这些分配(我假设您这样做;就目前而言,内存只是泄漏),您需要使用int**** &maze
。 (而且我仍然认为没有原始指针会更好,但这不是codereview.SE。)