从双指针数组中读取- NULL

Reading from double array of pointers - NULL

本文关键字:读取 NULL 数组 指针      更新时间:2023-10-16

我的方法替换双数组_board中的单元格。替换后,它将单元格设置为NULL。

在下一次调用设置为NULL的单元格时,它不会将其识别为NULL。

((_board [location.getX ()] [location.getY ()) = = NULL))

继续执行方法然后读取垃圾

什么错了吗?

    void Board::MovePiece(Point location, Move m) throw (GameEndException, OutOfBoundsException, InvalidMoveException, NonEmptySquareException)
{
    int i,j;
    bool flag1,flag2;
    if **(_board[location.getX()][location.getY()]==NULL)**{
        InvalidMoveException IO;
        throw IO;}
    if ((_board[location.getX()][location.getY()]->IsValidMove(m)==false)||(_board[location.getX()][location.getY()]->getPlayerNum()!=GetTurn())) {
        InvalidMoveException IO;
        throw IO;}
    if (_board[location.getDestination(m).getX()][location.getDestination(m).getY()]!=NULL) {
        if (_board[location.getDestination(m).getX()][location.getDestination(m).getY()]->getPlayerNum()==GetTurn()){
        NonEmptySquareException IO;
        throw IO;}}
        Point tmp=location.getDestination(m);
        _board[tmp.getX()][tmp.getY()]=_board[location.getX()][location.getY()]->Clone();
        delete _board[location.getX()][location.getY()];
        _board[location.getX()][location.getY()]=NULL;
    _lastM=m;
    _lastP=location;
                flag1=false;
                flag2=false;
        for (i=0;i<BOARD_SIZE;i++){
            for (j=0;j<BOARD_SIZE;j++){
                if(_board[i][j]!=NULL){
                if (_board[i][j]->getPlayerNum()==ONE) flag1=true;
                if (_board[i][j]->getPlayerNum()==TWO) flag2=true;
                }}}
        if ((flag1==false)||(flag2==false)){
            GameEndException IO;
        throw IO;}
}

这里delete _board[location.getX()][location.getY()];已删除位置,您正在尝试访问已删除的内存位置。