处理指针c ++的分段错误(这次更多代码)

时间:2013-12-11 01:58:29

标签: c++ pointers segmentation-fault

我希望有人可以帮助我解决我遇到的分段错误,这次我发布了更多代码,希望每个人都能看到我正在尝试做的事情。我确定这与我如何指向一切有关,因为那是我遇到麻烦的部分。这是.h

  using namespace std;
  // Data

  struct question{
  string programNum;
  string programDesc;
  string programPoints;
  string programInput;
  char* programQuestion;
  };

  void setQuestionFileName(question* q, char* fileName);
  void display(question* q);
  void display(question* q);

这是.cpp

using namespace std;


void setQuestionFileName(question* q, char* fileName){
    strcpy(q->programQuestion, fileName);
}

void display(question* q){

       cout << "Description           =  "   << q->programDesc     << endl;
       cout << "Number of Points      =  "   << q->programPoints   << endl;
       cout << "Name of Question File =  "   << q->programQuestion << endl;

}

// Not used or tested yet
int myCompare (const void * a, const void * b ) {
           const char *pa = *(const char**)a;
           const char *pb = *(const char**)b;

           return strcmp(pa,pb);
 }

和main.cpp:

 using namespace std;

 int main(int argc, char* argv[]){                                                              //or char** argv
 question* questions[argc-1];                                                           //Array of questions to be filled by loop.
 int sizeOfQuestions = argc;                                                                    //number of questions passed in at run time
 int numLines = 0;                                                                          //number of lines in file
 for(int i=0;i<argc;i++){                                                                   //Test loop to make sure the command line file names are read in
          std::cout << argv[i] << " says hello" << std::endl;
 }
 for(int count=0;count<sizeOfQuestions-1;count++){                                          //This loop places the information from the files into structs
      //char fileName = argv[count+1];
      char* fileName = argv[count+1];
      cout << "Problem number:  " << count+1 << "\t Working with file " << fileName << endl;
      std::fstream questionFile (fileName, std::fstream::in);                                    //Open the file
      if(questionFile.good()){
            cout << "File Opened" << endl;
            setQuestionFileName(questions[count],fileName);
            cout << questions[count]->programQuestion << endl;
            getline(questionFile,questions[count]->programNum);
            getline(questionFile,questions[count]->programDesc);
            getline(questionFile,questions[count]->programPoints);
            getline(questionFile,questions[count]->programInput);
            display(questions[count]);
            questionFile.close();
       }else{
            cout << "Could not open file!!!" << endl;
       }

  }

  return 0;

  }

最后,输出:

     $ ./a.exe q1.txt q2.txt
     ./a says hello
     q1.txt says hello
     q2.txt says hello
     Problem number:  1       Working with file q1.txt
     File Opened
     Segmentation fault (core dumped)

1 个答案:

答案 0 :(得分:1)

所以,根据你的问题的评论,我建议去了解调试器或习惯插入跟踪语句(例如cout&lt;&lt;“来到这里。”; function(); cout&lt;&lt; ;“也来到这里!”;等等。

在这种情况下,看起来question* questions[argc-1]会咬你。它只是创建一个指向question的指针数组而不是question数组。

你可以根据需要分配这些(不要忘记事后清理)或者使用能够更干净地管理生命周期的东西(查看std :: vector以获得基本的数组替换以及其他一些有用的特性)。

开始分配结构后,您会注意到您还没有在结构中分配programQuestion。可能是时候看一下这个类,或者看一下std :: string的工作方式。您可以很容易地从C字符串(指向char的指针)初始化。请参阅:Converting a C-style string to a C++ std::string