Thread: Unexplainable run-time segfault error

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Unexplainable run-time segfault error

    This is really bizarre... :wtf:
    Ok, so I have this function called "strIsFollowedByASpace()" which returns true if a given search string is followed by a space in a source string, else it returns false.
    At runtime in my program (since I managed to compile it fine), there was this segfault that kept cropping up, so I traced it to its source (by adding cout lines until I managed to ouput one of them), and the source was inside the strIsFollowedByASpace(). Note that this function had already been previously tested, and worked before. So I wasn't expecting for there to be an error in it, but I examined it more closely anyway once I discovered where the segfault was generating from. I had (since it was working) briefly modified it, but not anything that I thought would generate such an error. All I did was I changed some ints to unsigned ints. But anyway, I examined it anyway, and discovered where in the function the segfault was coming from, and tried to figure out a way to fix it, but could not. And you'll see why in a moment.

    I added some cout lines to the function to see if it entered an if statement that looks like this:

    Code:
    if (strOfSource.at(endIndex + 1) != ' ') { //the character after the last character of searchString in sourceString is not a space
        cout<< "sourceString[" << endIndex << " + 1] != ' '." <<endl;
        return false;
    }
    And it did not output that statement before seg-faulting, which gives evidence that it didn't enter it for some reason. Now here's why this is so weird:

    The same exact function/usecase works with no seg-fault when I tested it at codepad.org:

    C++ code - 56 lines - codepad

    I pass the same exact same c-string to the function as that passed in my real code, to try to replicate the real circumstances as much as possible, and there the function works as expected (no seg-fault)!
    In the real code, I pass to the function the buffer of a line read from file which contains the string "enum DAY {" (no quotes), which has 10 characters. It outputs the size of the sourceString (in my real code, as well as at the link I posted) as 10, which is correct, and it outputs the index of the last character of the search string (which is "enum") as 3. Hence, when I say access sourceString[endIndex + 1] that means in this case, it is sourceString[3 + 1] which is element 4. Since the array is 0-based, that is really character 5 (e = 0, n = 1, u = 2, m = 3, space-character = 4), which is indeed a space, and the codepad.org function proves this by doing the correct thing. That is why this is so bizarre in my function. And when I change the sourceString[endIndex + 1] line in my real code to sourceString[endIndex] instead, it no longer seg-faults, but of course it enters the wrong statement (i.e. if the "enum" string is not followed by a space) hence wrecking havoc on the result of my code.

    So I guess my question is: Why the heck is this function seg-faulting when there is clearly no problem with the code?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Hmm...nevermind. The function is working ok in the real code. It enters the else statement that indicates that the "enum" string IS followed by a space, thus returning true. The seg-fault appears to be somewhere else. Ignore my ramblings...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    What the hell! Ok, now we got something else unexplainable (for real, this time). This statement should NOT be entered on the first line read from file which contains the string "enum DAY {", but its entered anyway as evidenced by a debug cout statement I put inside it:

    Code:
    if (containsString(filestreamInBuffer, ";")) { //line which contains 'enum' contains the terminating semicolon also
                        cout<< "This statement has been entered anyway. WTF???" <<endl;
                        //do stuff...
    }
    Now my program outputs:

    This statement has been entered anyway. WTF???
    right before the seg-fault. Note that I just copied the containsString() function to codepad.org to double-check if its still working, and it is. When I pass to it the same exact string, the function returns false as it should:

    C++ code - 224 lines - codepad

    So now I have an if (false) statement that's being bizarrely entered despite being false. Can any of you geniuses explain this one to me?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, I added another cout statement inside the if statement, which outputs the content of the "filestreamInBuffer" c-string, and so it now outputs:

    This statement has been entered anyway. WTF???
    filestreamInBuffer contains: enum DAY {
    Did I break my compiler or what...?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When I compiled your code, it seems to run ok. no errors and does not enter the if() in main.
    What compiler and/or IDE are you using?

    Jim.

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    When I compiled your code, it seems to run ok. no errors and does not enter the if() in main.
    What compiler and/or IDE are you using?

    Jim.
    GCC / Code::Blocks

    Note that my whole program compiles fine. Its a run-time error/seg-fault we're talking about here.
    And its becoming a real pain in the ass...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you tried to DEBUG the program using the debugger and stepping through your code?

    Jim

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is the code being run within the
    if (searchStringIsSpecialString == 0)
    conditional the same in all 3 cases?

    If it is, then you really need to stop copy/pasting code and create a few more functions along the way.

    200+ lines and 10+ levels of indentation - no wonder you're confused.
    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.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Salem View Post
    Is the code being run within the
    if (searchStringIsSpecialString == 0)
    conditional the same in all 3 cases?
    Yes.
    If it is, then you really need to stop copy/pasting code and create a few more functions along the way.
    Lol, I've considered it.
    200+ lines and 10+ levels of indentation - no wonder you're confused.
    Actually, that's not the confusing part. In fact, none of my code is confusing (to me) except for the unexplainable part where an if(false) statement is entered.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    Have you tried to DEBUG the program using the debugger and stepping through your code?

    Jim
    That's no use because code execution doesn't even reach the functions we're talking about when the program is run with no arguments passed in.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If it's command line arguments you are missing the add them to the project (under Project - Set programs' arguments.

    Jim

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    If it's command line arguments you are missing the add them to the project (under Project - Set programs' arguments.

    Jim
    Thanks. I missed that feature.
    Yeah, that works.
    But its not like I don't already know where the problem begins. It begins at the if statement I already mentioned which is being entered even though the return value of the containsString() function is false.

    EDIT: Well, it did help me find the exact line that the seg-fault is on (on the code line right after the output lines I mentioned above).
    Last edited by Programmer_P; 06-25-2010 at 10:19 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > In fact, none of my code is confusing (to me) except for the unexplainable part where an if(false) statement is entered.
    Yeah - now try leaving it alone for a week.
    When you come back to it, you'll have an instant WTF moment.

    I might explain why, if I thought it would do any good....
    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.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So is this a problem of entering an if statement when it's not supposed to, or a segfault problem?
    For either problem please post the smallest piece of code that demonstrates the problem. The code you have posted does not seem to have any problems when I compile and run the code snippet. Also post a sample of any any files the program is reading and or writing and any command line arguments passed to the program.

    Jim

  15. #15
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Salem View Post
    > In fact, none of my code is confusing (to me) except for the unexplainable part where an if(false) statement is entered.
    Yeah - now try leaving it alone for a week.
    When you come back to it, you'll have an instant WTF moment.
    I doubt it. And I'm still not confused...
    By the sound of it though, you are confused by my code.
    I might explain why, if I thought it would do any good....
    Well, I might think it would do some good, if it weren't for the fact that I already know the answer to the why question.
    Oh, and for the record, I went ahead and followed your advice and put the "if (searchStringIsSpecialString" stuff in two other functions, which I then call from inside containsString().
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM