Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9280595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:54:33+00:00 2026-06-18T17:54:33+00:00

I am trying to read words in from a dictionary file and I am

  • 0

I am trying to read words in from a dictionary file and I am receiving a segfault on my 4th line reading in from the file. From what I have read about vectors in c++ to use the insert command I need to specify an iterator to use as a place holder. To do this is I instantiate an iterator at the beginning of my vector as such.

 vector<string>::iterator it = dictionaryFile.begin();

Inside of my for loop I increment where the iterator is pointing by using the increment operator

it++;

I am basing this off of the examples on http://www.cplusplus.com. The iterator works for the first 3 lines then the segfault occurs. I have double checked my file to make sure there are no erroneous characters that are being read in by getline. I believe this error has to do with the call to reserve that I make to ensure there is no overflow of the vector.

Since I am not using the keyword new when it is declared is the vector limited to a capacity of 5? I cannot find any examples of the new keyword being used though!

I have included the segment of code that causes this error. Please note I have re-designed this several times in an attempt to resolve this segfault and this was not my original implementation. I would appreciate any insight a master of c++ might have. Thank you for your time.

vector<string> dictionaryFile (5, "");                                                                     //Declaration of vector that will hold the words in the dictionary

ifstream input;                                                                                                  //Declaration of input file stream
input.open(inst.c_str());

/********************** Test to see if file was opened ********************************/                
if(!input){
    cerr << "The file " << inst <<" does not exists in this directory" << '\n';
}
/********************** File is successfully opened**********************************/

string temporaryProcessingString = "";                                                                //This string will temporarily hold values read in from the file
vector<string>::iterator it = dictionaryFile.begin();                                                 //Creates iterator to step through the vector and fix this wild shit

for(int i = 0; getline(input, temporaryProcessingString); i++){                                 //See I can follow directions given in class no eof used.
    cout << "Does this print before SegFault 1 " << endl;
    if(dictionaryFile.size() >= (dictionaryFile.capacity() - dictionaryFile.size()) ){       //If current size is greater the 50% capacity reserve size*2 more memory
        int oldSize;
        oldSize = dictionaryFile.size();
        cout << "Does this print before SegFault 2 " << endl;
        dictionaryFile.reserve(oldSize + oldSize);                                                  //Reservation new capacity for vector containing dictionary
    }

/** this is a test bracket that solely keeps track of the vectors size and capacity in the terminal COMMENT OUT BEFORE SUBMITTING*/
cout << "________________________________________________" << '\n';
cout << "Loop Run: " << i << endl;
cout << "Size: " << dictionaryFile.size() << ", Capacity: " << dictionaryFile.capacity() << endl;
cout << "________________________________________________" << '\n';
dictionaryFile.insert(it, temporaryProcessingString); /*******************************THIS LINE CAUSES THE SEGFAULT! UNFORTUNATELY IT IS ALSO THE LINE THAT MOVES THE DATA INTO THE VECTOR************************/
it++;
cout << "Dictionary Entry: " << dictionaryFile[i] << endl;

}
cout << "Dictionary Entry Primary Test: " << dictionaryFile[0] << endl;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:54:34+00:00Added an answer on June 18, 2026 at 5:54 pm

    When your vector’s size approaches its current capacity (or, in your case, when you call vector::reserve, see the section on iterator validity here), an internal algorithm will re-allocate it and potentially move it somewhere else, making all iterators to elements in the vector invalid.

    As your program only seems to insert at the end anyways, use vector::push_back instead of vector::insert to prevent this. You can then also skip the reserveing – the internal algorithms are quite good (you don’t have to be afraid of the vector doing reallocation for every single new element – the algorithms are smarter than that).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read some words from an online text file. I tried
I'm trying to read a line of text from a text file and put
I am trying to read a list of words from a file in C++.
I'm trying to read a list of words from a txt file into a
I am trying to read from a text file and use the input to
I am trying to read a text file from raw folder, and it works
What I am trying to do is, Open the word file, read a line,
I am trying to read in an XML file that I have saved to
I am trying to read a large list of English words from a text
I'm trying to just get rid of duplicate consecutive words from a text file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.