Like Tree3Likes
  • 3 Post By grumpy

Segmentation fault - can't find it

This is a discussion on Segmentation fault - can't find it within the C++ Programming forums, part of the General Programming Boards category; I have a function that compresses string and one function reading textfiles to strings. There's some issue that I can't ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    110

    Segmentation fault - can't find it

    I have a function that compresses string and one function reading textfiles to strings. There's some issue that I can't nail. The idea is to read a textfile line by line and put each line into a string vector. When I dump the vector each line seems to be propper but when I try to use the vector in an function I get segmentation fault. Adding to my confusion - if I hard code the strings insterad of using file2string str2comp works.

    The two functions are as follows.
    Code:
    string str2comp(std::string str, int offset = 0){
    
    ........string compstr;
    ........string::iterator it;
    ........int index = offset;
    
    ..for ( it = str.begin() ; it < str.end(); ++it){
    ................if (index%4 == 0){
    .. ..................compstr += (char)map.find(str.substr(index,4))->second;
    .... ........}
    ....................index++;
    .... }
    
    ........return compstr;
    }

    Code:
    string file2String(const string &fileName){
    ....ifstream ifs(fileName.c_str(), ios::in | ios::binary | ios::ate);
    ....if(!ifs)
    .... ..{
    .... .. ..cerr << "Error : file2String() could not open file: " << fileName << endl;
    .... .. ..exit(1);
    .... ..} else {
    .... ..ifstream::pos_type fileSize = ifs.tellg();
    .... ..ifs.seekg(0, ios::beg);
    .... ..vector<char> bytes(fileSize);
    .... ..ifs.read(&bytes[0], fileSize);
    .... ..ifs.close();
    .... ..return string(&bytes[0], fileSize);
    ....}
    }
    I'm kind of green when it comes to C++ and I would really appreciate feedback or help.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,899
    Have you tried running your program through your debugger? The debugger should be able to tell you exactly where the problem is detected. Plus you should be able to view the variables at the time of the crash. Without a complete program, finding a segmentation fault is very difficult.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    You are aware we're not mind readers?

    Without some representative code that calls those functions, and exhibits your problem, you rely on us having mindreading skills.

    Try creating a SMALL but COMPLETE sample of code that exhibits the symptoms. In the process of producing that sample, you might have an "aha!" moment. If not, you can post the code, and mere mortals might have a chance of helping you.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Sure...

    Code:
    string = file2String("filename");
    compstr = string2comp(string,0);

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    How about properly indenting your code, rather than filling it with useless dots.

    The next thing, run the program in the debugger, and it will point you at the line of code causing the fault, and give you the chance to look around and find out why it's crashing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Quote Originally Posted by Salem View Post
    How about properly indenting your code, rather than filling it with useless dots.

    The next thing, run the program in the debugger, and it will point you at the line of code causing the fault, and give you the chance to look around and find out why it's crashing.
    About that debugging, any names or links to the software?

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,899
    Quote Originally Posted by überfuzz View Post
    Sure...

    Code:
    string = file2String("filename");
    compstr = string2comp(string,0);
    How does the above relate to a SMALL but COMPLETE program?

    About that debugging, any names or links to the software?
    What compiler are you using?

    Jim

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    I write:..make Aligner && ./Aligner in the bash window..

    Edit, I'm trying to help some biologist, one being my girlfriend, with some analysis on their cluster. I'm not really used to C++ and I thought my errors would, or hoped they would be quite evident. That's why I only posted the functions messing things up.
    Last edited by überfuzz; 11-29-2012 at 12:47 PM.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    If you're using a modern IDE then it should have a debugger already built-in.

    As for the file2String function there are easier/shorter ways of reading the entire content of a file into a string container (if I judge your intent with that function correctly) by way of using a stringstream container as an intermediate storage object:
    Code:
    std::ifstream file("your_file_name_goes_here");
    std::stringstream sstr;
    std::string data;
     
    sstr << file.rdbuf();  // Get whole file into stringstream object sstr
    data = sstr.str();  // Convert into string, data now contains entire file's content
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Quote Originally Posted by jimblumberg View Post
    How does the above relate to a SMALL but COMPLETE program?
    It doesn't.

    The str2comp() function is also referring to a map, which is not declared, let alone defined in any code provided by the OP.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    The map:
    Code:
    void buildmap(){
    
    
             map["AAAA"]=0;
             map["AAAT"]=1;
    ..        etc...
             map["CCCC"]=255;
             
    }

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    Some reading material for you here.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Quote Originally Posted by hk_mp5kpdw View Post
    If you're using a modern IDE then it should have a debugger already built-in.
    I'm sitting by an old unix system, I have no permissions nor do I have an insight on what softwares available. Thanks anyway!
    As for the file2String function there are easier/shorter ways of reading the entire content of a file into a string container (if I judge your intent with that function correctly) by way of using a stringstream container as an intermediate storage object:
    Code:
    std::ifstream file("your_file_name_goes_here");
    std::stringstream sstr;
    std::string data;
    ..
    sstr << file.rdbuf(); ..// Get whole file into stringstream object sstr
    data = sstr.str(); ..// Convert into string, data now contains entire file's content
    Cheers!

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    As already posted in the other thread: map.find(str.substr(index,4))->second may crash, if map.find(...) returns map.end(). You should compare it to that before you dereference the iterator.
    

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Do you have gdb?

    A tutorial (one of many, this is just google's lucky hit)
    gdb Tutorial
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault using recursion to find factorial
    By kapok in forum C++ Programming
    Replies: 4
    Last Post: 02-23-2009, 11:10 AM
  2. Can't find the cause of a segmentation fault
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 02-06-2006, 08:28 PM
  3. Help me rid this segmentation fault
    By Nornny in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2004, 07:42 PM
  4. segmentation fault
    By js_badboy in forum C Programming
    Replies: 2
    Last Post: 09-11-2003, 07:59 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

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