![](/img/trans.png)
[英]I have a hangman game I'm making, and I need help figuring out why it's not fully working
[英]Need help figuring out why I'm getting this segmentation fault
我正在為C ++中的MIPS管道模擬器編寫代碼。 我的功能之一是獲取。 經過一些調試后,我縮小到了發生分段錯誤的提取功能。 有人可以幫我弄清楚為什么會發生嗎? 代碼如下:
void Simulator::fetch(){
int flag =0;
string buf, rd;
int i;
for(i = 0;i<4;i++){
if(pre_issue_buffer[i]==";"){
flag = 1;
break;
}
}
if(flag ==1){
if(i<3){
string instr = memory.read_memory(PC);
stringstream ss(instr);
vector<string> tokens;
while (ss >> buf)
tokens.push_back(buf);
string instruction = tokens.at(0);
if(instruction == "BREAK"){
brk =1;
instr_string=instruction;
}
else if(instruction=="NOP"){
instr_string=instruction;
}
else if(instruction=="J"){
int address=toInt(tokens.at(1));
if(address>this->break_addr){
cerr<<"Invalid Jump Address at: "<<PC<<endl;
}
PC = address;
exec_instr=instruction+"\t#"+tokens.at(1);
}
else if(instruction=="JR"){
rd = tokens.at(1);
if(regInUse[rd]==0){
p=regFile.find(tokens.at(1));
PC = p->second;
exec_instr=instruction+"\t"+tokens.at(1);
}
else
waiting_instr= instruction+"\t"+tokens.at(1);
}
else if(instruction=="BEQ"){
int rs,rt;
rd = tokens.at(1);
if(regInUse[rd]==0){
p=regFile.find(tokens.at(1));
rs = p->second;
p=regFile.find(tokens.at(2));
rt = p->second;
if(rs==rt){
int offset=toInt(tokens.at(3));
PC = PC+offset+4;
}
else
PC=PC+4;
exec_instr=instruction+"\t"+tokens.at(1)+", "+tokens.at(2)+", #"+tokens.at(3);
}
else
waiting_instr=instruction+"\t"+tokens.at(1)+", "+tokens.at(2)+", #"+tokens.at(3);
}
else if(instruction=="BLTZ"){
rd = tokens.at(1);
if(regInUse[rd]==0){
p = regFile.find(tokens.at(1));
int rs = p->second;
if(rs<0){
int offset=toInt(tokens.at(2));
PC = PC + offset+4;
}
else
PC=PC+4;
exec_instr=instruction+"\t"+tokens.at(1)+", #"+tokens.at(2);
}
else
waiting_instr=instruction+"\t"+tokens.at(1)+", #"+tokens.at(2);
}
else if(instruction=="BGTZ"){
rd = tokens.at(1);
if(regInUse[rd]==0){
p = regFile.find(tokens.at(1));
int rs = p->second;
if(rs>0){
int offset=toInt(tokens.at(2));
PC = PC + offset+4;
}
else
PC=PC+4;
exec_instr=instruction+"\t"+tokens.at(1)+", #"+tokens.at(2);
}
else
waiting_instr=instruction+"\t"+tokens.at(1)+", #"+tokens.at(2);
}
else{
rd = tokens.at(1);
pre_issue_buffer[i]=instr;
cout<<i<<endl;
PC=PC+4;
}
}
對於每個數組,檢查要嘗試訪問的位置,並驗證它是否在數組范圍內。
沒有足夠的信息來回答。 據我所知,它可能在功能的開始或結束。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.