从双数组指针读取 - NULL

时间:2014-06-23 13:12:30

标签: c++

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

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

((_板[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;}
}

1 个答案:

答案 0 :(得分:1)

此处delete _board[location.getX()][location.getY()];您已删除该位置,并且您正尝试访问已删除的内存位置。