调试ECX_BAD_ACCESS错误C

Debugging ECX_BAD_ACCESS error C++

本文关键字:错误 ACCESS BAD ECX 调试      更新时间:2023-10-16

有什么想法如何通过以下代码修复以下错误?这是在Xcode的C 环境中。

    //
//  main.cpp
//  Chess
//
//  Created by Akshar Ramkumar on 9/29/16.
//  Copyright © 2016 Akshar Ramkumar. All rights reserved.
//
#include "Declarations.hpp"
#include "DeclarationsMain.hpp"
extern simplePiece TotalBoard[8][8];
using namespace std;
int main() {


    Piece All[32];
    start(All);
    char rawinput[4];
    char fxinput;
    char fyinput;
    char txinput;
    char tyinput;
    while(true){
        Turn=not Turn;
        if(Turn==true){
            TurnColor="white";
        }

        else {
            TurnColor="black";
        }
        cout<<"It is "<<TurnColor<<"'s turn"<<endl;
        cout<<"Enter the x and y coordinates you want to move from 0-7, 0-7 (no spaces in between)"<<endl;
        cin>>rawinput;
        fxinput=rawinput[0];
        fyinput=rawinput[2];
        cout<<"Enter the x and y coordinates you want to move to 0-7, 0-7 "<<endl;
        cin>>rawinput;
        txinput=rawinput[0];
        tyinput=rawinput[2];
        if(TotalBoard[fxinput][fyinput].Color==Turn and TotalBoard[fxinput][fyinput].Color==true){
            TotalBoard[txinput][tyinput]=TotalBoard[fxinput][fyinput];
            TotalBoard[fxinput][fyinput].Color=false;
            TotalBoard[fxinput][fyinput].Type=0;
            TotalBoard[fxinput][fyinput].exists=false;



        }

    }

    return 0;
}

下一步:

//
//  Classes.cpp
//  Chess
//
//  Created by Akshar Ramkumar on 10/13/16.
//  Copyright © 2016 Akshar Ramkumar. All rights reserved.
//Pawn = 0
//Rook = 1
//Knight = 2
//Bishop = 3
//King = 4
//Queen = 5
#include "Declarations.hpp"
#include "DeclarationsBoard.hpp"
void start(Piece All[32]){
    int TypeArray[32];
    int xValues[32];
    int yValues[32];
    std::ifstream startstate;
    std::fstream boardstate;
    std::string numread;
    char delim=' ';
    startstate.open("/Users/aksramk/Google Drive/For Fun/Programming/Pascal C++/Chess/boardstatestart.txt")
    ;
    boardstate.open("/Users/aksramk/Google Drive/For Fun/Programming/Pascal C++/Chess/boardstatecurrent.txt");
    for(int i=0;i<32;i++){
        getline(startstate, numread,delim);

        TypeArray[i]=atoi(numread.c_str());
    }
    for(int i=0;i<32;i++){
        getline(startstate, numread, delim);

        xValues[i]=atoi(numread.c_str());
    }
    for(int i=0;i<32;i++){
        getline(startstate, numread, delim);

        yValues[i]=atoi(numread.c_str());
    }

    for (int i=0;i<32;i++){

        All[i].Type = TypeArray[i];
        All[i].y = yValues[i];
        All[i].x = xValues[i];
        All[i].Color = true;
        All[i].Captured = false;

        if (i>15){
            All[i].Color = false;
        }
    }
    startstate.close();
    for(int i=0;i<32;i++){
        boardstate<<TypeArray[i];
        boardstate<<" ";
    }
    for(int i=0;i<32;i++){
        boardstate<<xValues[i];
        boardstate<<" ";
    }
    for(int i=0;i<32;i++){
        boardstate<<yValues[i];
        boardstate<<" ";
    }
    for(int i=0;i<32;i++){
        boardstate<<0;
        boardstate<<" ";
    }
    boardstate.close();
    ;
    for(int i=0;i<8;i++){
        for(int j=0;j<8;j++){
            simplePiece temp;
            for(int k=0;k<32;k++){
                Piece temp2=All[k];
                if(temp2.x==i and temp2.y==j){
                    temp.Type=temp2.Type;
                    temp.Color=temp2.Color;
                    temp.exists=true;
                }
            }
            TotalBoard[i][j]=temp;
        }
    }

    };

标题:

//  DeclarationsMain.h
//  Chess
//
//  Created by Akshar Ramkumar on 11/22/16.
//  Copyright © 2016 Akshar Ramkumar. All rights reserved.
//
#ifndef DeclarationsMain_h
#define DeclarationsMain_h
bool Turn=false;
std::string TurnColor;
#endif /* DeclarationsMain_h */

下一步:

  //
//  DeclarationsBoard.h
//  Chess
//
//  Created by Akshar Ramkumar on 11/22/16.
//  Copyright © 2016 Akshar Ramkumar. All rights reserved.
//
#ifndef board
#define board
simplePiece TotalBoard[8][8];
#endif 

下一步:

//
//  DataStructures.hpp
//  Chess
//
//  Created by Akshar Ramkumar on 10/18/16.
//  Copyright © 2016 Akshar Ramkumar. All rights reserved.
//

#ifndef iostream
#define iostream
#include <iostream>
#include <fstream>

#endif
#ifndef piece
#define piece
class Piece {
    public:
        int Type;
        int x;
        int y;
        bool Captured;
        bool Color;

};
struct simplePiece{
    bool Color=false;
    int Type=0;
    bool exists=false;

};
void start(Piece All[32]);
#endif

行:

TotalBoard[txinput][tyinput]=TotalBoard[fxinput][fyinput];

引发错误:"线程1:ecx_bad_access(代码= 2,地址= 0x100009bac)。

有人对如何解决这个问题有任何想法吗?

非常感谢,对不起一个简单的问题(我是初学者)。

您代码的相关部分:

char rawinput[4];
cin>>rawinput;
fxinput=rawinput[0];
fyinput=rawinput[2];

此处的输入操作在该字符数组中读取"单词"(本质上是没有空格的文本)。假设用户输入" 1 2"。这里的第一个单词是" 1"。然后,rawinput数组保存由" 1"的ASCII值组成的C字符串,然后是Nullbyte。

rawinput[0]是49。

rawinput[2]然后是一些不确定的值


一种解决方案是使用int变量:

int x;
int y;
cin >> x >> y;

相关文章: