调试ECX_BAD_ACCESS错误C
Debugging ECX_BAD_ACCESS error C++
有什么想法如何通过以下代码修复以下错误?这是在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;
相关文章:
- 我在发布模式下运行时收到"Access violation reading location"错误 - C++
- Java 本机访问代码错误:"Invalid memory access"
- 将数据添加到 CArray 会产生错误"cannot access private member declared in class 'CObject'"
- 如何修复我的 c++ 毕达哥拉斯三重查找器中的'access violation reading location'错误?
- 矩阵 - "Access violation writing location 0x00900B0C" - C++ 中的错误
- c++:链表错误" Access violation reading location"
- msftedit "Access violation reading location 0x00000008"错误.dll SetWindowSub类中的RichEdit控件
- 从 UMDF 驱动程序调用创建文件时出现'Access is denied'错误 (C++)
- 指针已初始化,但我有错误"Access violation reading location 0xCCCCCCCC".为什么?
- 文件系统项目"Member access into incomplete type"错误
- 从C程序访问C++函数时,收到错误消息"Access violation reading location"
- 常见日志文件系统 API 的 RegisterManageableLogClient() 和"access is denied"错误
- 推力变换引发错误:"bulk_kernel_by_value: an illegal memory access was encountered"
- 为什么装配中会出现"Access violation reading location"错误?
- 简单的 CUDA 测试总是失败并出现错误"an illegal memory access was encountered"
- MFC 设计器中的"Cannot access ActiveX Control type library"错误
- 仅当类具有导出链接时"cannot access private member'"错误
- 我收到此错误"Unhandled exception at 0x009437bd in Hash functions.exe: 0xC0000005: Access violation reading
- 如何解决OpenCV中的"Access violation reading location"错误?
- 错误:在 .../boost/serialization/access.hpp:132 上未定义对"bbque::Event::Event()"的引用