【发布时间】:2015-01-08 06:38:23
【问题描述】:
尝试访问时,我的程序中出现“访问冲突读取位置”错误 我的 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);
}
我不希望您通读所有这些代码,但无论如何我都发布了。如果有人知道我做错了什么,你能指出我正确的方向吗?
感谢您的宝贵时间 贾斯汀韦克, 顺便说一句,我真的不想使用向量,并且可以正确处理定位我的内存,绝对没有内存泄漏。
【问题讨论】:
-
你不能避免使用 4D 阵列迷宫..
-
我正在使用 4D 数组,因为我正在以块的形式生成迷宫以进行优化,这样我就可以使迷宫大于 21^2,而无需大量加载。
-
好吧,让我举个例子,内存可以像 a[m*n] 和 a[m][n] 那样分配,首先是一维数组,其次是二维数组,理想情况下它使代码更多超过 2D 令人困惑且难以调试 ...
-
我还没有阅读整个代码,但首先要指出的是:你将
int**** mazes传递给mazeGen,但该函数所做的第一件事就是丢弃该值传入并用新的分配替换它。如果您希望调用者可以看到这些分配(我假设您这样做;就目前而言,内存只是泄漏),您需要使用int**** &maze。 (而且我仍然认为没有原始指针会更好,但这不是 codereview.SE。) -
但有时需要大于 2D 的数组,而且动态数组已经无法在 MS VS 中调试。
标签: c++ multidimensional-array access-violation dynamic-arrays 4d