[英]Segmentation fault
#include <iostream>
using namespace std;
void initializeMap(int mapSizeX, int mapSizeY, int map[][10])
{
// Map details:
// 0 = # (wall)
// 1 = space (free space)
// 2 = x (player)
for(int x = 0; x < mapSizeX; x++)
{
map[x][0] = 0;
}
for(int y = 0; y < (mapSizeY - 2); y++)
{
map[0][y] = 0;
for(int x = 0; x < (mapSizeX - 2); x++)
{
map[x][y] = 1;
}
map[mapSizeX][y] = 0;
}
for(int x = 0; x < mapSizeX; x++)
{
map[x][mapSizeY - 1] = 0;
}
}
void paintMap(int mapSizeX, int mapSizeY, int map[][10])
{
for(int y = 0; y < mapSizeY; y++)
{
for(int x = 0; x < mapSizeX; x++)
{
switch(map[x][y])
{
case 0:
cout << "#";
break;
case 1:
cout << " ";
break;
case 2:
cout << "x";
break;
}
cout << map[x][y];
}
cout << endl;
}
}
int main()
{
int mapSizeX = 10;
int mapSizeY = 10;
int map[10][10];
initializeMap(mapSizeX, mapSizeY, map);
paintMap(mapSizeX, mapSizeY, map);
cout << endl << endl;
return 0;
}
My code compiles perfectly fine without errors but when I try to run it, it just says "Segmentation fault".我的代码编译得非常好,没有错误,但是当我尝试运行它时,它只是说“分段错误”。 I've done some research and I don't understand why I get it because I don't use pointers at all.
我做了一些研究,但我不明白为什么我得到它,因为我根本不使用指针。 How do I fix this?
我该如何解决? I compile it using g++ and run it by just typing./main in the terminal.
我使用 g++ 编译它并通过在终端中键入 ./main 来运行它。
map[mapSizeX][y] = 0;
This is illegal.这是非法的。 Valid values of the index run from
0
to mapSizeX - 1
.索引的有效值从
0
到mapSizeX - 1
。
The line should be:该行应该是:
map[mapSizeX][y] = 0;
One assumes that this is the desired output?假设这是所需的 output?
#0#0#0#0#0#0#0#0#0#0 #0 1 1 1 1 1 1 1 1#0 #0 1 1 1 1 1 1 1 1#0 #0 1 1 1 1 1 1 1 1#0 #0 1 1 1 1 1 1 1 1#0 #0 1 1 1 1 1 1 1 1#0 #0 1 1 1 1 1 1 1 1#0 #0 1 1 1 1 1 1 1 1#0 #0 1 1 1 1 1 1 1 1#0 #0#0#0#0#0#0#0#0#0#0
If so, you have a number of other off-by-one errors in your initializeMap
function.如果是这样,您的
initializeMap
function 中还有许多其他错误。 Instead of:代替:
for(int y = 0; y < (mapSizeY - 2); y++)
and和
for(int x = 0; x < (mapSizeX - 2); x++)
you should use你应该使用
for(int y = 1; y < (mapSizeY - 1); y++)
and和
for(int x = 1; x < (mapSizeX - 1); x++)
respectively.分别。
BTW, here's a cleaner way to write initializeMap
:顺便说一句,这是编写
initializeMap
的一种更简洁的方法:
template<int mapSizeX, int mapSizeY>
void initializeMap(int (&map)[mapSizeX][mapSizeY])
{
for( int y = 0; y < mapSizeY; y++ ) {
for( int x = 0; x < mapSizeX; x++ ) {
if (x == 0 || x + 1 == mapSizeX || y == 0 || y == mapSizeY)
map[x][y] = 0;
else
map[x][y] = 1;
}
}
}
And you can call it with just你可以用它来调用它
initializeMap(map);
No need to pass the size, the compiler will figure it out automatically.无需传递大小,编译器会自动计算出来。
I didn't look deep enough to know whether this is the only problem with your code, but我没有深入了解这是否是您的代码的唯一问题,但是
map[mapSizeX][y] = 0;
will write above the bounds of the array.将写在数组的边界之上。
In the second for loop of initializeMap
-在
initializeMap
的第二个for循环中 -
map[mapSizeX][y] = 0;
mapSizeX
is 10 and there is no 10*y
element in the matrix. mapSizeX
为 10,矩阵中没有10*y
元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.