程序接收信号EXC_BAD_ACCESS - Mac OS X.

时间:2013-08-04 16:45:07

标签: c++

当我将std :: vector中的元素与char进行比较时,我有时会在运行到程序时随机出现此错误(大约每五次一次):

  

编程收到信号EXC_BAD_ACCESS,无法访问内存。

     

原因:地址为13:0x0000000000000000

     

0x0000000100017c49 in randomCaveGenerator(p = @ 0x7fff5fbff51c,q = @ 0x7fff5fbff518,seed = 1234567)at src / main.cpp:198   198 if(lm.map [1] [i] [j] =='0')

v被定义为std :: vector,其中填充了所有元素。我知道这个错误通常意味着它指向一个空指针,但我知道不应该有一个

我正在使用Mac OS X 10.8,gdb进行调试,并使用Apple LLVM 4.2版(基于LLVM 3.2svn)进行编译。

修改

我正在使用的代码:

typedef std::vector< std::vector<char> > MapGrid;

struct LevelMap
{
    std::string name;
    std::vector<MapGrid> map;
    sf::Vector2u size;
};


LevelMap randomCaveGenerator(int p=64, int q=64, unsigned long seed=1)
{

    if (p < 64)
         p = 64;
    if (q < 64)
         q = 64;

    int groundLevel = 12;

    srand(seed);

    sf::Vector2u size(p, q);
    std::vector<MapGrid> mapG(3);
    for (int i = 0; i < 3; i++)
    {
        mapG[i] = MapGrid(size.x, std::vector<char>(size.y));

        for (int y = 0; y < size.y; y++)
        {
            for (int x = 0; x < size.x; x++)
            {
                 mapG[i][x][y] = '0';
            }
        }
    }

    for (int y = 0; y < size.y; y++)
    {
        for (int x = 0; x < size.x; x++)
        {
            mapG[0][x][y] = 'v'; // Void Tile
        }
    }
    for (int y = 0; y < size.y; y++)
    {
        for (int x = 0; x < size.x; x++)
        {
            mapG[2][x][y] = '0'; // Air Tile
        }
    }

    for (int y = groundLevel; y < size.y; y++)
    {
        for (int x = 0; x < size.x; x++)
        {
            int e = 23;
            if (2+(rand()%e)< e/2)
                mapG[1][x][y] = 'd';
            else
                mapG[1][x][y] = '0';

        }
    }

    LevelMap lm;
    lm.name = "Random Map";
    lm.map = mapG;
    lm.size = size;

    for (int i = 0; i < 12; i++)
        doSimStep(lm);

    for (int i = 1; i < lm.size.x - 2; i++)
    {
        for (int j = 1; j < 12+groundLevel; j++)
        {
            if (lm.map[1][i][j] == '0') // PART WHERE THE ERROR IS
                continue;
            else
            {
                lm.map[1][i][j] = 'g';
                lm.map[2][i][j-1] = 'w';
                i++;
                j = 1;
            }
        }
    }

    for (int i = 1; i < lm.size.x - 2; i++)
    {
        for (int j = groundLevel; j < lm.size.y - 2; j++)
        {
            if (lm.map[1][i][j] == 'd')
            {
                if (rand()%120 == 0)
                    lm.map[2][i][j] = 'p';
            }
        }
    }

    for (int i = 0; i < lm.size.y; i++)
    {
        lm.map[1][0][i] = 'X';
        lm.map[1][lm.size.x-2][i] = 'X';
        lm.map[2][0][i] = '0';
        lm.map[2][lm.size.x-2][i] = '0';

    }

    for (int i = 0; i < lm.size.x; i++)
    {
        lm.map[1][i][0] = 'X';
        lm.map[1][i][lm.size.y-2] = 'X';
        lm.map[2][i][0] = '0';
        lm.map[2][i][lm.size.y-2] = '0';

    }

    return lm;
}

1 个答案:

答案 0 :(得分:3)

除了错误消息

之外没有源代码
 if (v[i] == '0')

我只能想象i >= v.size()

你可能想考虑使用迭代器来访问你的向量,所以你不要走到最后。