Jump to content
  • Advertisement
Sign in to follow this  
fredrikhcs

segmentation fault after cin

This topic is 3713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a really simple program. All it does so far is go into main and then do this:
string word;
vector<string> words;

while (cin >> word) words.push_back(word);
However, when running the code, when that while loop is exited---either by EOF (CTRL-D) or if I do something like "if (word == "somethingofarbitraryvalue") break;"---I get a segmentation fault. What am I doing wrong here?

Share this post


Link to post
Share on other sites
Advertisement
I just tested your code on g++ 3.4.0 and got no segmentation fault. If that exact code (with the include directives and using-statements required) causes you a segmentation fault, reinstall your entire compilation suite (and corresponding libraries) and if it persists submit a bug to your compiler writer.

Share this post


Link to post
Share on other sites
Quote:
Original post by fredrikhcs
I'm writing a really simple program. All it does so far is go into main and then do this:
*** Source Snippet Removed ***

However, when running the code, when that while loop is exited---either by EOF (CTRL-D) or if I do something like "if (word == "somethingofarbitraryvalue") break;"---I get a segmentation fault. What am I doing wrong here?


Random segmentation faults are almost always caused by either an invalid pointer, or writing memory somewhere you're not supposed to be writing to. Is the while loop the last part of main()? If so, and if it is crashing as soon as you exit the loop (and hence main), it sounds like you're somehow writing into the stack and overwriting your return address.

Can you post your entire program please?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!