Like Tree1Likes
  • 1 Post By tabstop

Can't solve a certain seg fault.

This is a discussion on Can't solve a certain seg fault. within the C++ Programming forums, part of the General Programming Boards category; I'm currently writing part of a lab program for a data structures class I'm taking. I'm only partway into the ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    17

    Can't solve a certain seg fault.

    I'm currently writing part of a lab program for a data structures class I'm taking. I'm only partway into the lab, so there's not much written at the moment.

    First, let me apologize in advance. This code will be a bit messy, as the last two CS classes I had were not in C++, and it's been almost a year since I've coded much of anything (last semester's class was Computer Organization, taught with MIPS32 and such).

    I'm encountering a seg fault. The gdb debugger's report on the segfault is as follows:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    __strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:31
    31      ../sysdeps/x86_64/multiarch/../strlen.S: No such file or directory.
            in ../sysdeps/x86_64/multiarch/../strlen.S
    I have narrowed down the search to what seems to be the problem code, but I don't see anything wrong with it... The problem code, and the related variable, are underlined.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    class Song{
        public:
            string title;
            int time;
            int track;
    };
    
    class Album{
        public:
            map <int, Song*> songs;
            string name;
            int time;
    };
    
    class Artist{
        public:
            map <string, Album*> albums;
            string name;
            int time;
            int nsongs;
    };
    
    int main(int argc, char* argv[])
    {
        ifstream fin;
        string filename;
        int min;
        int sec;
        int time;
        size_t found;
        string s;
        Song* curSong;
    
        filename = argv[1];
        if(argc == 2 && filename.find(".txt") != string::npos){
            fin.open(argv[1]);
    
            if(fin.fail()){
                cerr << "Problem opening " << filename << endl;
                exit(1);
            }
        }
        else{
            cerr << "Please use format: lib_info filename.txt\n";
            exit(1);
        }
    
        while(!fin.eof()){
            fin >> s;
            for(found = s.find("_"); found != string::npos; found = s.find("_")) {
                s.replace(found, 1, " ");
            }
            curSong->title = s;
    
            fin >> s;
            sscanf(s.c_str(), "%d:%d", &min, &sec);
            curSong->time = min*60 + sec;
    
            fin >> s;
    
            fin >> s;
    
            fin >> s;
    
            fin >> s;
            sscanf(s.c_str(), "%i", &(curSong->track));
        }
    
        return (0);
    }
    I'm unfamiliar with s.find and string::npos. I pulled them off a reference site, and attempted to use them, so I wouldn't be surprised if that's where the problem was.

    In case it's needed, here's a sample input file.
    Code:
    Countdown 2:25 Coltrane,_John Giant_Steps Jazz 3
    Down_In_Brazil 6:07 Walton,_Cedar Naima Jazz 4
    Giant_Steps 4:02 Puente,_Tito El_Rey Jazz 5
    Giant_Steps 4:46 Coltrane,_John Giant_Steps Jazz 1
    Mr._P.C. 7:02 Coltrane,_John Giant_Steps Jazz 7
    Naima 4:24 Coltrane,_John Giant_Steps Jazz 6
    Naima 5:16 Lyle,_Bobby Night_Breeze Jazz 5
    Naima 5:36 Tjader,_Cal A_Fuego_Vivo Jazz 6
    Naima 7:49 Walton,_Cedar Naima Jazz 6
    Naima 8:38 Walton,_Cedar Eastern_Rebellion Jazz 2
    This_Guy's_In_Love_With_You 8:10 Walton,_Cedar Naima Jazz 2

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So when your compiler says
    Code:
    temp.cpp:66:37: warning: `curSong' may be used uninitialized in this function
    that should maybe suggest to you that you should initialize curSong before you try to use it on line 66.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    17
    Quote Originally Posted by tabstop View Post
    So when your compiler says
    Code:
    temp.cpp:66:37: warning: `curSong' may be used uninitialized in this function
    that should maybe suggest to you that you should initialize curSong before you try to use it on line 66.
    That...would be a good idea, yeah.

    Actually, my compiler never gave me that warning.

    However, it actually seg faults before it ever tries to use curSong.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's not clear from what you posted that the segfault is before that line, and in fact I doubt that it is. I suspect you are looking at the last successful command, rather than the first broken command (since the illegal access is directly after checking found != string::npos).

    (And EDIT: You should also then either get a real compiler or learn to use the one you have so that you get useful warnings/error messages.)

    And ooh another EDIT: You are also using eof() to control a loop, which is going to do exactly what you don't want it to (namely: not stop when you are out of data). I have no idea what the state of string s is when fin>>s fails -- I believe it to just continue to contain the previous data -- but nevertheless you will want to fix that. (The usual method is to put the fin>>s as the controlling expression of the while loop.)
    Last edited by tabstop; 08-25-2011 at 09:38 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    Quote Originally Posted by MSZShadow
    Actually, my compiler never gave me that warning.
    Increase your compiler's warning level.
    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

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    17
    Quote Originally Posted by tabstop View Post
    It's not clear from what you posted that the segfault is before that line, and in fact I doubt that it is. I suspect you are looking at the last successful command, rather than the first broken command (since the illegal access is directly after checking found != string::npos).

    (And EDIT: You should also then either get a real compiler or learn to use the one you have so that you get useful warnings/error messages.)
    As part of the process for narrowing down where the error was, I placed a cout statement on the line immediately before the line where I attempted to use curSong. It seg faulted before it printed the short message.

    Also, I'm using the Unix g++ compiler. It frequently gives warnings/errors, but didn't give that one for whatever reason.

    Edit: I allocated room for curSong, and the segfault is now gone. Thanks for pointing that out.

    That makes me wonder why the message didn't print before seg faulting, though...
    Last edited by MSZShadow; 08-25-2011 at 09:41 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you remember to put \n or std::endl at the end of it? I'll bet you a quarter you didn't.

    If you're using g++ and not using -Wall and -Wextra and -Weffc++, then you're just being silly.

    Also: see other EDIT above.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Increase your compiler's warning level.
    Quote Originally Posted by tabstop View Post
    If you're using g++ and not using -Wall and -Wextra and -Weffc++, then you're just being silly.

    Also: see other EDIT above.
    I'll look into those. Thanks.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Quote Originally Posted by MSZShadow View Post
    As part of the process for narrowing down where the error was, I placed a cout statement on the line immediately before the line where I attempted to use curSong. It seg faulted before it printed the short message.
    Ah, then you're assuming that it would print the message and then segfault. That isn't necessarily going to be what happens because cout is buffered and dealt with asynchronously.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by iMalc View Post
    Ah, then you're assuming that it would print the message and then segfault. That isn't necessarily going to be what happens because cout is buffered and dealt with asynchronously.
    Would putting a flush after cout change that ?
    Last edited by manasij7479; 08-26-2011 at 05:15 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    It should, hence the question of whether std::endl was used.
    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

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    -Wall is a necessity, -Wextra a good idea, but the value -Weffc++ is debatable.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by MSZShadow View Post
    As part of the process for narrowing down where the error was, I placed a cout statement on the line immediately before the line where I attempted to use curSong. It seg faulted before it printed the short message.

    Also, I'm using the Unix g++ compiler. It frequently gives warnings/errors, but didn't give that one for whatever reason.

    Edit: I allocated room for curSong, and the segfault is now gone. Thanks for pointing that out.

    That makes me wonder why the message didn't print before seg faulting, though...
    What is your updated code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is error, kindly solve the issue and make me know my fault
    By kapil1089thekin in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2010, 11:27 PM
  2. Can anybody let me know how to solve this.
    By Farhan in forum C Programming
    Replies: 5
    Last Post: 02-24-2009, 07:44 PM
  3. WHO can solve this???
    By scjohnny in forum C++ Programming
    Replies: 6
    Last Post: 08-12-2008, 09:40 AM
  4. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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