【问题标题】:Need help figuring out why I'm getting this segmentation fault需要帮助弄清楚为什么我会遇到这个分段错误
【发布时间】:2012-04-11 18:34:10
【问题描述】:

我正在用 C++ 为 MIPS 管道模拟器编写代码。我的功能之一是获取。经过一些调试,我缩小到我的 fetch 函数,其中发生了分段错误。有人可以帮我弄清楚为什么会这样吗?代码如下:

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

}

【问题讨论】:

  • 你编译调试了吗?你有核心转储吗?您可以尝试使用调试器(例如 gdb)让机器人更接近错误。
  • 我如何使用gdb来接近错误,我没有使用gdb太多,我使用backtrace发现错误在这个函数中,但是我该如何超越呢?跨度>

标签: c++ segmentation-fault computer-architecture


【解决方案1】:

为每个数组检查您尝试访问的位置并验证它是否在数组边界内。

我们没有足够的信息来回答。据我所知,它可能在你函数的开始和结束时。

【讨论】:

  • 好的,我在这里使用的唯一数组是 pre_issue_buffer,它最多可以包含 4 个元素。 regInUse 和 Memory 是映射对象,我之前已经测试过它们,所以我不希望它们会产生错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多