【问题标题】:C++ crash Segmentation fault: 11C++ 崩溃分段错误:11
【发布时间】:2014-02-01 19:38:42
【问题描述】:

我一直在开发一个程序来测试一些字符串操作的可能性。它基本上应该读取字符串列表并能够找到字符的邻居以通过字符串作为电路。代码如下:

    #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”。我正在使用一些我不习惯并且可能滥用的主题和技术。任何帮助都会很棒。

【问题讨论】:

  • 在什么编译器下失败了?使用 g++4.8(经过小修复 - 它没有编译!),它可以工作 coliru.stacked-crooked.com/a/bbc1a7134ee38989,但没有输出。
  • 是的,我应该提到这一点。我在一个带有 10.9 Mavericks 和 Apple LLVM 版本 5.0 (clang-500.2.79) 的 Mac 上。我可以通过删除 getNeighbors() 的每个调用和引用来停止运行时错误,但这就是程序的重点并控制输出。

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


【解决方案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];
        }
      }
    }

neighborsnow 是全球性的(我不喜欢这样,但为 POC 做这项工作)。

【讨论】:

  • 您使用向量作为参数,但我的变量是 std::string*,我将其用作数组。你能解释一下如何用我的代码实现你的向量吗?
【解决方案2】:

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

【讨论】:

  • 我尝试将邻居声明为任何函数之外的全局变量,并在 getNeighbors() 中使用 ::neighbors 引用它。这没有效果,仍然返回分段错误 11。
猜你喜欢
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 2014-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多