C ++崩溃分段错误:11

时间:2014-02-01 19:38:42

标签: c++ crash segmentation-fault runtime-error

我一直致力于一个程序来测试一些字符串操作的可能性。它基本上应该读取一个字符串列表,并能够找到一个字符的邻居作为电路通过字符串。这是代码:

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>

    std::string grid[20]={" "};

    std::string get(int string, int member){
      return grid[string].substr(member,1);
    }
    std::string* getNeighbors(int string, int member){
      std::string neighbors[4];
      neighbors[0]=grid[string-1].substr(member,1);//up
      neighbors[1]=grid[string+1].substr(member,1);//down
      neighbors[2]=grid[string].substr(member-1,1);//left
      neighbors[3]=grid[string].substr(member+1,1);//right
      std::string* p=neighbors;
      return p;//Returns up,down,left,right.
    }
    int main(int argc, char** argv){
      grid[1]="@----^---0";
      grid[2]="abcdefghi0";
      grid[3]="jklmnopqr0";//TODO Change to read of txt*/
      std::string* neighbors;
      for(int i=0;grid[1].length()>i;i++){
        neighbors=getNeighbors(2,1);
        if(neighbors[3]=="-" | neighbors[3]=="^"){
          std::string r=get(1,i);
          (r!="0") ? std::cout<<r:0;//Dangerous. TODO Unknown symbol handling
          std::cout<<neighbors[3];
        }
      }
    }

编译得很好,但有运行时错误“Segmentation fault:11”。我正在使用我不习惯的几个主题和技巧,并且可能会误用。任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

std::string neighbors[4];是堆栈分配的。当你出去getNeighbors时,它会失去范围。试着把它放在其他地方(甚至是全球性的,只是作为概念的证明)。一个更好的设计应该作为你的功能的旁观者。

void getNeighbors(int string, int member, std::vector<std::string>& neighbors){
      ;
      neighbors[0]=grid[string-1].substr(member,1);//up
      neighbors[1]=grid[string+1].substr(member,1);//down
      neighbors[2]=grid[string].substr(member-1,1);//left
      neighbors[3]=grid[string].substr(member+1,1);//right
    }

编辑:

#include <iostream>
    #include <string>
    #include <sstream>
    #include <fstream>

    std::string grid[20]={" "};
    std::string neighbors[4]; //<---------------------------

    std::string get(int string, int member){
      return grid[string].substr(member,1);
    }
    std::string* getNeighbors(int string, int member){
      neighbors[0]=grid[string-1].substr(member,1);//up
      neighbors[1]=grid[string+1].substr(member,1);//down
      neighbors[2]=grid[string].substr(member-1,1);//left
      neighbors[3]=grid[string].substr(member+1,1);//right
      std::string* p=neighbors;
      return p;//Returns up,down,left,right.
    }
    int main(int argc, char** argv){
      grid[1]="@----^---0";
      grid[2]="abcdefghi0";
      grid[3]="jklmnopqr0";//TODO Change to read of txt*/
      std::string* neighbors;
      for(int i=0;grid[1].length()>i;i++){
        neighbors=getNeighbors(2,1);
        if(neighbors[3]=="-" | neighbors[3]=="^"){
          std::string r=get(1,i);
          (r!="0") ? std::cout<<r:"0";//Dangerous. TODO Unknown symbol handling
          std::cout<<neighbors[3];
        }
      }
    }

neighbors现在是全球性的(我不喜欢这个,但为POC做的工作)。

答案 1 :(得分:0)

getNeighbors()返回一个指向局部变量的指针。