Segmentation error in vector?! How could this happen?

This is a discussion on Segmentation error in vector?! How could this happen? within the C++ Programming forums, part of the General Programming Boards category; I used below codes to pass a value from vector to my cpp file. While i was doing that, i ...

  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
    21,662
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21