A. Ramkumar A. Ramkumar - 7 days ago 5
C++ Question

Debugging ECX_BAD_ACCESS error C++

Any ideas how to fix the following error with the following code? This is in a C++ environment in Xcode.

//
// 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;
}


Next:

//
// 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;

}
}


};


Headers:

// 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 */


Next:

//
// 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


Next:

//
// 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


The line :

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


throws the error: "Thread 1: ECX_BAD_ACCESS (code=2, address=0x100009bac).

Does anyone have any ideas about how to fix this?

Thanks so much, and sorry for the simple question (I am a beginner).

Answer

Relevant parts from your code:

char rawinput[4];

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

The input operation here reads a "word" (essentially text without spaces) into that character array. Let's say the user inputs "1 2". The first word here is "1". The rawinput array then holds a C string consisting of the ASCII value for "1", followed by a nullbyte.

rawinput[0] is then 49.

rawinput[2] is then some indeterminate value, whatever was there before.


One solution is to use int variables:

int x;
int y;

cin >> x >> y;