Segmentation Fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flyvin
    New Member
    • Oct 2008
    • 11

    Segmentation Fault

    I have a vector full of vectors, and whenever I try to access them I get a segmentation fault. It compiled fine, but then when I ran it I got a error. So I ran a debuger and it said a had a segmentation fault. here is the code:
    Code:
    vector< vector<string> > questions;
    //some other code where I add things to the vector
    
    for (int i=0; i < questions.size(); i+=1){
               for (int k=0; k < questions[i].size(); k+=1){
                            cout << questions[i][k]; 
               }
    }
    Please help.
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    When I fill your vector with strings and then print out the vector with the code you have there, I do not get a segmentation fault. The problem must be in some other part of the code. Can you please post more of the code?
    Thanks.

    Comment

    • flyvin
      New Member
      • Oct 2008
      • 11

      #3
      Here is all the code that uses the vector. What I'm doing is getting questions from a text file. The part that says "<<<~~~BREAK~~~ >>>" is just something to separate the questions.

      Code:
       string StrTmp;
             int IntTmp;
             
             vector< vector<string> > questions;
      };
             
      Dimo::Dimo(){
                   ifstream questionsF("mem/questions.srj");
                   string question;
                   if (questionsF.is_open()){
                            while(questionsF.eof()==false){
                                                          getline(questionsF, question);
                                                          vector<string> questionV;
                                                          if (question == "<<<~~~BREAK~~~>>>"){
                                                                       questions.push_back(questionV);
                                                                       questions.clear();
                                                                       continue;
                                                          }
                                                          questionV.push_back(question);
                            }
                            
                            for(int i=0; i < questions.size(); i+=1){
                                     for (int k=0; k < questions[i].size(); k+=1){
                                         cout << questions[i][k]; 
                                     }
                            }
                                
                   }else{
                         cout << "There was a problem loading some memory";
                   }
                   
      
                   questionsF.close();
                         
               
                   
      };

      Comment

      • flyvin
        New Member
        • Oct 2008
        • 11

        #4
        Ok, weird now its working. Must have just been something wrong with my computer for a while. Thanks anyway.

        Comment

        Working...