Sign in to follow this  
fredrikhcs

segmentation fault after cin

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this