簡體   English   中英

這段代碼一次執行良好,另一次出現分段錯誤

[英]This piece of code executes fine once and gives segmentation fault the other time

我有寶石對象 gems[10][10],它有一個屬性顏色。 我想在具有相同顏色的列中獲得三個或更多連續的寶石,並改變它們的顏色。 下面的代碼一次運行良好,而另一次出現分段錯誤。 是什么導致隨機運行中的分段錯誤。

    bool findMatch(){
        for(int i=0;i<10;++i){      // To check ForVertical matchces
            for(int j=1;j<10;++j){
                int count=0;
                int a=0;
                if(gems[j][i].getColor()==gems[j-1][i].getColor()){
                    a=j;
                    count++;
                    while(gems[a][i].getColor()==gems[j][i].getColor() && a<10){
                        count++;
                        a++;
                    }
                }
             if(count>=3){
                 for(int x=j-1, itr=0;itr<count;++itr,++x){
                     gems[x][i].setColor(7);
                 }
                    glutPostRedisplay();
                 for(int x=j-1, itr=0;itr<count;++x,++loop){
                     gems[x][i].setColor(GetRandInRange(0,7));
                 };
                 return true;
             }
            }
        }
return false;
}
 while(gems[a][i].getColor()==gems[j][i].getColor() && a<10){

a達到 10 的值時,這里。 gems[10]a<10比較之前被評估。 由於這超出了數組的大小,因此會導致未定義的行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM