簡體   English   中英

C ++崩潰分段錯誤:11

[英]C++ crash Segmentation fault: 11

我一直在開發一個程序,以測試一些字符串操作的可能性。 基本上應該讀取字符串列表,並能夠找到角色的鄰居以將其作為電路進行遍歷。 這是代碼:

    #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”。 我正在使用一些我不習慣的主題和技術,並且可能會濫用。 任何幫助都會很棒。

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做這件事)。

getNeighbors()返回一個指向局部變量的指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM