Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Segmentation Fault

    In this main function, I get a segmentation fault whenever I enter "add //somenumber". If I use the other commands (remove, print, quit), the code works fine. I have the cout<<input right after getline is called to see the input. It will cout all the input options (including JUST "add") except when I input "add //somenumber". This is the code -

    Code:
     
        bool stop = false;
        UnorderedList uList;
    
        string input;
        do {
            try {
                cout<<"Enter a command"<<endl;
                getline(cin, input);
    
                cout<<input;
    
                if(strncmp(input.c_str(), "add", 3) == 0) {
                    cout<<"in add if";
                    int num;
                    if(input.size() <= 4 || input.at(3) != ' ' || input.at(4) == ' ')
                        throw UnorderedList::InvalidInputException();
                    else {
                        cout<<"in else";
                        num = atoi(input.substr(4, input.size()-4).c_str());
                        cout<<num;
                        uList.add(num);
                    }
                }   //end add
    
                else if(strncmp(input.c_str(), "remove", 6) == 0) {
                    int num;
                    if(input.size() <= 7 || input.at(6) != ' ' || input.at(7) == ' ')
                        throw UnorderedList::InvalidInputException();
                    else {
                        num = atoi(input.substr(7, input.size()-7).c_str());
                        uList.remove(num);
                    }
                }   //end remove
    
                else if(strncmp(input.c_str(), "print", 5) == 0)
                    cout<<uList.toString();
    
                else if(strncmp(input.c_str(), "quit", 4) == 0)
                    stop = true;
                else
                    throw UnorderedList::InvalidInputException();
    
    
            }
            catch(UnorderedList::EmptyListException &ex) {cout<<ex.what()<<endl;}
            catch(UnorderedList::InvalidInputException &ex) {cout<<ex.what()<<endl;}
        } while(!stop);
    I don't see why I am getting a segmentation error. And what confuses me the most is that apparently the fault would occur at the getline call...since the cout<<input does not get called. Can someone tell me why this fault is happening? Thanks for any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to use output for debugging, make sure you call flush() on your output stream. You have no idea whether cout << input or cout << "in add if" happen, since they won't get seen until you print a new line or flush the output stream.

    (Also you don't have to give a second argument to substr, if you want your substring to go all the way to the end.)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Hmm...well I used the debugger and it tells me that the add function gets called. It stops on the setNext() function
    Code:
    void UnorderedList::add(int& element0) {
        Node* temp = new Node(element0);
        temp->setPrevious(tail);
        tail->setNext(temp);  //****HERE*****
        tail = temp;
        count++;
    }   //END ADD
    The setNext and setPrevious functions are simply
    Code:
    void UnorderedList::Node::setNext(Node* n) {next = n;}
    void UnorderedList::Node::setPrevious(Node* n) {previous = n;}
    Apparently the code gets past the setPrevious function fine. Could it be that passing the local Node* temp to the setNext function is the problem?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should make sure tail exists before trying to dereference it with tail->setNext. (For instance, if you try to add to an empty list, this code will fail horribly since tail will be NULL.)

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Ah okay that makes sense.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Isn't it strange to have a segmentation fault? What is your platform (what OS, What compiler)? How you get the error?
    Last edited by siavoshkc; 03-28-2010 at 08:47 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by siavoshkc View Post
    Isn't it strange to have a sementation error? What is your platform (what OS, What compiler)? How you get the error?
    It's not really that strange.

    Most variants of unix operating systems send a signal named SIGSEGV (typically signal number 11) to a running program that is detected accessing memory it is not allowed to. The default action for a program receiving that signal is an abnormal termination, reporting a segmentation violation, and, (typically) dumping a core file.

    Other operating systems typically cause misbehaving programs (those that access a forbidden area of memory) to exit, but the problem reported may be different. For example, under windows, the equivalent of a segmentation fault is often seen as a "General Protection Fault".

    In C or C++ code, almost any operation that writes to memory it shouldn't can cause such a symptom.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Quote Originally Posted by grumpy
    Other operating systems typically cause misbehaving programs (those that access a forbidden area of memory) to exit, but the problem reported may be different. For example, under windows, the equivalent of a segmentation fault is often seen as a "General Protection Fault".
    Isn't it an Access Violation Exception(0x5) in Windows?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Quote Originally Posted by siavoshkc View Post
    Isn't it strange to have a segmentation fault? What is your platform (what OS, What compiler)? How you get the error?
    Yeah I just recently switched to Linux and had never really seen the error before. Now it seems almost every program I have write at some point gives me a segmentation fault. Right now I'm using Ubuntu Linux, Code::Blocks IDE, and GNU GCC compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM