Thread: Segmentation error in vector?! How could this happen?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    1

    Segmentation error in vector?! How could this happen?

    I used below codes to pass a value from vector to my cpp file. While i was doing that, i got segmentation fault in my main cpp? Is there anyway to pass the vector value to my main cpp? thx for the help

    Code:
    void Loadfile()
    {
    	//cout<<"this is used for loading file"<<endl;//check if the codes passing here
    	string line;
        ifstream myfile;
        vector <string> myvector;
        
    	myfile.open(DATAFILE);    // Open the datafile
    	if (myfile.is_open())     //check If it worked
        {
    			while (getline(myfile,line,'\n'))//get whole line from DATAFILE 
               {
    											//cout<<i++<<endl;//to test if the codes work correctly
                 myvector.push_back(line);		//store them in vector
                }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bigbrain
    I used below codes to pass a value from vector to my cpp file. While i was doing that, i got segmentation fault in my main cpp?
    If I were to rewrite it with better indentation and following the rule of thumb that variables should be declared near first use:
    Code:
    void Loadfile()
    {
        ifstream myfile(DATAFILE);
        if (myfile.is_open())
        {
            vector<string> myvector;
            string line;
            while (getline(myfile, line, '\n'))
            {
                myvector.push_back(line);
            }
    Frankly, this code snippet looks okay. The problem probably lies in the code that you did not show, perhaps related to your next question...

    Quote Originally Posted by bigbrain
    Is there anyway to pass the vector value to my main cpp?
    You could return a copy of the vector from Loadfile (potentially expensive option), or you could pass the vector in by reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    A crash in a vector usually indicates you are attempting to access an index that does not exist or if you have pointers in the vector that those pointers are pointing to invalid memory and thus when you attempt to use the pointers you get a crash.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  3. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM