2013-08-04 44 views
0

當我在性病比較的元素::矢量爲char,我隨機有時運行程序時出現此錯誤(約每5次):程序接收到的信號EXC_BAD_ACCESS - Mac OS X的

程序收到信號EXC_BAD_ACCESS,無法訪問內存。

原因:13在地址:0x0000000000000000

在randomCaveGenerator 0x0000000100017c49(p值= @ 0x7fff5fbff51c,Q = @ 0x7fff5fbff518,種子= 1234567)在SRC/main.cpp中:198 198如果(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; 
} 
+2

我們不可能在沒有看到您的代碼的情況下幫助您。 – Borgleader

+2

着名遺言「我*知道*不應該有一個」。當然這意味着*是問題所在:你沒有看到它,因爲你知道它不在那裏。 – wallyk

+1

很可能,'i'超出了有效範圍。但是發佈代碼,我們也許可以提供一個更好的主意。 –

回答

3

由於沒有源代碼,除了錯誤消息

if (v[i] == '0') 

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

你可能想要考慮r使用迭代器來訪問你的矢量,所以你不會走到最後。

+2

就像'我<0'一樣。 – wallyk

+1

或'v'某種程度上「破碎」。 –

+0

哦,問題中的錯誤現在已經改變,但它是相似的。我放棄 – doctorlove

相關問題