【问题标题】:Run-Time Check Failure #2 - Stack around the variable 'positions' was corrupted运行时检查失败 #2 - 变量“位置”周围的堆栈已损坏
【发布时间】:2011-05-02 08:45:46
【问题描述】:

我在返回结构时遇到问题。它说运行时检查失败 #2 - 变量“位置”周围的堆栈已损坏,我不知道为什么。我必须在今晚之前完成这项工作,才能继续该项目的其余部分。如果您知道,请提供帮助。谢谢你:)

struct Position
{
    int SrcDstScore[50][5];
    char pieceColor [50][2];
};

int Sort(Position s2, Position s1)
{
    int x; // use compreto method
    return x;
}
Position EvaluateMoves(Piece * theBoard[8][8])
{   
    //store my result boards here 
    Position positions;
    for (int t=0; t<50; t++)
    {
        positions.SrcDstScore[t][1]= 0;
        positions.SrcDstScore[t][2]= 0;
        positions.SrcDstScore[t][3]= 0;
        positions.SrcDstScore[t][4]= 0;
        positions.SrcDstScore[t][5]= 0;
        positions.pieceColor[t][1]= ' ';
        positions.pieceColor[t][2]= ' ';
        //cout << "Eval";
    }
    char mcPlayerTurn = 'w';
    int counter = 0;
    // Fetching the board containing positions of these pieces
       for (int Row = 0; Row < 8; ++Row) 
        {
            for (int Col = 0; Col < 8; ++Col)   
            {
                if (theBoard[Row][Col] != 0)
                {
                    if (theBoard[Row][Col]->GetColor() == 'w')
                    {
                        Piece * piece =  theBoard[Row][Col];
                        for (int MoveableRow = 0; MoveableRow < 8; ++MoveableRow) // Now that we have found the knight find the legal squares. 
                        {
                            for (int MoveableCol = 0; MoveableCol < 8; ++MoveableCol)   
                            {
                                if(piece->IsLegalMove(Row, Col, MoveableRow, MoveableCol, theBoard))
                                {
                                    cout << counter << theBoard[Row][Col]->GetPiece() << " " << theBoard[Row][Col]->GetColor() <<" " <<  Row <<" " <<  Col <<" " <<  MoveableRow <<" " <<  MoveableCol << "\n";
                                    positions.SrcDstScore[counter][1]= Row;
                                    positions.SrcDstScore[counter][2]= Col;
                                    positions.SrcDstScore[counter][3]= MoveableRow;
                                    positions.SrcDstScore[counter][4]= MoveableCol; 

                                    //If the move is a capture add it's value to the score
                                    if (theBoard[MoveableRow][MoveableRow] != 0)
                                    {
                                        positions.SrcDstScore[counter][5] += theBoard[MoveableRow][MoveableRow]->GetValue();

                                        if (theBoard[Row][Row]->GetValue() < theBoard[MoveableRow][MoveableRow]->GetValue())
                                        {
                                            positions.SrcDstScore[counter][5] += theBoard[MoveableRow][MoveableRow]->GetValue() - theBoard[Row][Row]->GetValue();
                                        }
                                        //Add Score for Castling
                                    }
                                    counter ++;
                                }
                            }
                        }
                    }
                }
            }
       }

        return positions;
     }

【问题讨论】:

    标签: c++ structure


    【解决方案1】:

    在初始循环中,您将分别使用索引 1 到 5 和 1 到 2 对 SrcDstScorepieceColor 进行索引。这些应该是索引 0 到 4 和 0 到 1。

    这同样适用于更下方,在这一行和之后的三行:

    positions.SrcDstScore[counter][1]= Row;
    

    在您拥有positions.SrcDstScore[counter][5] 的两个实例中,应该是...[4]

    【讨论】:

      【解决方案2】:
       positions.pieceColor[t][1]= ' ';
       positions.pieceColor[t][2]= ' ';
      

      应该是-

       positions.pieceColor[t][0]= ' ';
       positions.pieceColor[t][1]= ' ';  // Array index starts from 0 to numElements - 1.
      

      Position::SrcDstScore 也是如此。数组索引从 0 到 4 开始。

      【讨论】:

      • @GeeKaush - 通常,当您遇到此类运行时错误时,表明变量试图访问未分配到的位置。
      • @GeeKaush - 很高兴我能帮助你。
      【解决方案3】:

      如果变量theBoard 是二维数组或指向二维数组Piece 的指针,维度为8 * 8(用于棋盘),则函数声明应如下所示:

      Position EvaluateMoves(Piece (*theBoard)[8])  // 1st way
      Position EvaluateMoves(Piece theBoard[][8])  // 2nd(a) way
      Position EvaluateMoves(Piece theBoard[8][8])  // 2nd(b) way
      Position EvaluateMoves(Piece (&theBoard)[8][8])  // 3rd way, preferred if theBoard is an array
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        • 2013-12-13
        • 2013-12-10
        • 2015-02-09
        • 2015-05-24
        • 2021-12-31
        • 2014-10-20
        相关资源
        最近更新 更多