简体   繁体   中英

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.

map[mapSizeX][y] = 0;

This is illegal. Valid values of the index run from 0 to mapSizeX - 1 .

The line should be:

map[mapSizeX][y] = 0;

One assumes that this is the desired 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. 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 :

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 -

map[mapSizeX][y] = 0;

mapSizeX is 10 and there is no 10*y element in the matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM