C++ 从双指针数组读取-NULL
我的方法替换双阵列板中的单元。 替换后,它将单元格设置为空 在对设置为NULL的单元格的下一次调用中,它不会将其识别为NULL (_板[location.getX()][location.getY()]==NULL)) 然后继续用这种方法读垃圾 怎么了C++ 从双指针数组读取-NULL,c++,C++,我的方法替换双阵列板中的单元。 替换后,它将单元格设置为空 在对设置为NULL的单元格的下一次调用中,它不会将其识别为NULL (_板[location.getX()][location.getY()]==NULL)) 然后继续用这种方法读垃圾 怎么了 void Board::MovePiece(Point location, Move m) throw (GameEndException, OutOfBoundsException, InvalidMoveException, NonE
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;}
}
void Board::MovePiece(点位置,移动m)抛出(GameEndException,OutOfBoundsException,InvalidMoveException,NoneEmptySquareException)
{
int i,j;
布尔flag1、flag2;
如果**(_board[location.getX()][location.getY()]==NULL)**{
invalidMoveIO;
抛出IO;}
if((_board[location.getX()][location.getY()]->IsValidMove(m)=false)| |(_board[location.getX()][location.getY()]->getPlayerNum()!=GetTurn()){
invalidMoveIO;
抛出IO;}
如果(_board[location.getDestination(m).getX()][location.getDestination(m).getY()]!=NULL){
如果(_board[location.getDestination(m).getX()][location.getDestination(m).getY()]->getPlayerNum()==GetTurn()){
非空方异常IO;
抛出IO;}}
点tmp=位置.getDestination(m);
_线路板[tmp.getX()][tmp.getY()]=\u线路板[location.getX()][location.getY()]->Clone();
删除_板[location.getX()][location.getY()];
_线路板[location.getX()][location.getY()]=NULL;
_lastM=m;
_lastP=位置;
flag1=假;
flag2=假;
对于(i=0;igetPlayerNum()=TWO)flag2=true;
}}}
如果((flag1==false)| |(flag2==false)){
配子异常;
抛出IO;}
}
此处删除[location.getX()][location.getY()]
您已删除该位置,并且正在尝试访问已删除的内存位置。之后,该方法立即将其设置为NULL,顺便说一句,即使没有此代码行,它也无法工作。现在出现了什么问题?你能详细说明一下错误的具体位置吗?在第二次运行时,它会到达被删除的单元格。。。行:_board[location.getX()][location.getY()]==NULL不是真的。它认为这是错误的。但在第一次运行时,它将其设置为NULL_板子是双指针数组不要删除单元格,逻辑中一定有问题,如果不知道在第一次/第二次运行中如何调用它以及传递的值,我就无法判断。恐怕这无法通过代码检查。