简体   繁体   English

C ++崩溃分段错误:11

[英]C++ crash Segmentation fault: 11

I have been working on a program to test out a few string manipulation possibilities. 我一直在开发一个程序,以测试一些字符串操作的可能性。 It is basically supposed to read a string list and be able to find a character's neighbors to go through the strings as a circuit. 基本上应该读取字符串列表,并能够找到角色的邻居以将其作为电路进行遍历。 Here's the code: 这是代码:

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

This compiles well, but has the runtime error "Segmentation fault: 11". 这样可以很好地编译,但是运行时错误为“ Segmentation fault:11”。 I am using several subjects and techniques that I am not used to and am likely misusing. 我正在使用一些我不习惯的主题和技术,并且可能会滥用。 Any help would be great. 任何帮助都会很棒。

std::string neighbors[4]; is stack allocated. 是堆栈分配的。 When you go out getNeighbors it looses scope. 当您外出使用getNeighbors它将失去作用域。 Try to put it other place (even globaly, just as a proof of concept). 尝试将其放到其他位置(甚至是全局性,以作为概念证明)。 A better design should be pass this as parater to your function. 更好的设计应将此作为对您的功能的约束。

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
    }

EDIT: 编辑:

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

The neighbors now is global (I don´t like this, but do the job for the POC). 现在, neighbors是全球性的(我不喜欢这样,但是要为POC做这件事)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM