Thread: Unexplainable run-time segfault error

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by jimblumberg View Post
    So is this a problem of entering an if statement when it's not supposed to, or a segfault problem?
    Both. The segfault problem seems to be buried in my strippedString() function, which I'm debugging right now.
    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
    I will. Just a sec...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, here is the problem code:

    Code:
    if (containsString(filestreamInBuffer, ";")) { //line which contains 'enum' contains the terminating semicolon also
        cout<< "This statement has been entered anyway. WTF???" <<endl;
        cout<< "filestreamInBuffer contains: " << filestreamInBuffer <<endl;
        currentEnumName = stripString(filestreamInBuffer, 'm', '{'); //we now have the name of the current enum
        //continue doing stuff inside this if statement...
    }
    Ok, now my output statements demonstrate that that if statement is being entered (even though it is clear that the return value of the containsString() function here is false, but the if statement is still entered).
    My debugger shows that the line which produces the segfault is the bolded line of code, so the problem appears to be inside stripString().
    But the output shows this:

    This statement has been entered anyway. WTF???
    filestreamInBuffer contains: enum DAY {
    stripString() has been entered.
    sizeOfStr is: 10
    str[3] == start
    str[4] != end
    str[5] != end
    str[6] != end
    str[7] != end
    str[8] != end
    str[9] == end
    strippedString is: DAY
    Segmentation fault
    And here is the definition of the stripString() function (note that I narrowed the last output line before the segfault I posted to the else statement at the end of the function):
    Code:
    const string& CenumOperations::stripString(const char str[], const char start, const char end, ET_strippingMode strippingMode) {
    
        cout<< "stripString() has been entered." <<endl;
        unsigned int sizeOfStr = 0;
        while (str[sizeOfStr] != '\0') {
            sizeOfStr++; //increment sizeOfStr
        }
        cout<< "sizeOfStr is: " << sizeOfStr <<endl;
        bool startHasNotBeenReachedYet = true;
        bool endHasNotBeenReachedYet = true;
        unsigned int i2 = 0;
        string strippedString = ""; //create an empty string
        for (unsigned int i = 0; i < sizeOfStr; i++) {
            //loop without doing anything until 'start' character is found or until the end of the string has been reached...
            if (str[i] == start) {
                cout<< "str[" << i << "] == start" <<endl;
                startHasNotBeenReachedYet = false; //since 'start' has now been found
                if (strippingMode == exclusive) {
                    i2 = i+1; //begin the inner do-while loop counter at the index of the next character after the 'begin' character
                }
    
                else if (strippingMode == inclusive) {
                    i2 = i; //begin the inner do-while loop counter at the index of the 'begin' character
                }
    
                do {
                    if (i2 < sizeOfStr - 1) { //we're not at the last character of the string yet
                        if (strippingMode == exclusive) { //meaning we want to exclude the 'begin' and 'end' characters from stripped string
                            if (str[i2] == end) { //the current character matches the 'end' character
                                cout<< "str[" << i2 << "] == end" <<endl;
                                endHasNotBeenReachedYet = false; //since we do not want to add the 'end' character to the stripped string
                                cout<< "strippedString is: " << strippedString <<endl;
                                return strippedString;
                            }
    
                            else if (str[i2] != end) { //the current character does not match the 'end' character
                                cout<< "str[" << i2 << "] != end" <<endl;
                                if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
                                    strippedString += str[i2]; //add the current character of the string to the stripped string
                                }
                            }
                        }
    
                        else if (strippingMode == inclusive) { //meaning we want to include the 'begin' and 'end' characters from stripped string
                            if (str[i2] == end) { //the current character matches the 'end' character
                                cout<< "str[" << i2 << "] == end" <<endl;
                                if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
                                    strippedString += str[i2]; //add the 'end' character to the stripped string
                                }
                                endHasNotBeenReachedYet = false; //since we added the 'end' character and we're done
                            }
    
                            else if (str[i2] != end) { //the current character does NOT match the 'end' character
                                cout<< "str[" << i2 << "] != end" <<endl;
                                if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
                                    strippedString += str[i2]; //add the remaining character we want to add
                                }
                            }
                        }
                    }
    
                    else if (i2 == sizeOfStr - 1) { //we're at the last character of 'str'...
                        if (strippingMode == exclusive) { //meaning we want to exclude the 'begin' and 'end' characters from stripped string
                            if (str[i2] == end) { //the last character of the string matches the 'end' character
                                cout<< "str[" << i2 << "] == end" <<endl;
                                endHasNotBeenReachedYet = false; //since we do not want to add the 'end' character to the stripped string
                                cout<< "strippedString is: " << strippedString <<endl;
                                return strippedString;
                            }
    
                            else if (str[i2] != end) { //the current character does not match the 'end' character
                                cout<< "str[" << i2 << "] != end" <<endl;
                                if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
                                    strippedString += str[i2]; //add the last character of the string to the stripped string
                                }
    
                                break; //out of the loop, since we just added the last character (if it wasn't a space)
                            }
                        }
    
                        else if (strippingMode == inclusive) { //meaning we want to include the 'begin' and 'end' characters from stripped string
                            if (str[i2] == end) { //the last character of the string matches the 'end' character
                                cout<< "str[" << i2 << "] == end" <<endl;
                                strippedString += str[i2]; //add the 'end' character to the stripped string
                                endHasNotBeenReachedYet = false; //since we added the 'end' character and we're done
                            }
    
                            else if (str[i2] != end) { //the last character of the string does NOT match the 'end' character
                                cout<< "str[" << i2 << "] != end" <<endl;
                                if (str[i2] != ' ') { //since we don't want spaces in the stripped string...
                                    strippedString += str[i2]; //add the last character of the string to the stripped string
                                }
    
                                break; //out of the loop, since we just added the last character
                            }
                        }
                    }
    
                    i2++; //increment the inner-do while loop iterator
                } while(endHasNotBeenReachedYet);
    
                break; //out of the for loop since we already scanned through the rest of the string after 'start'
            }
    
        }
    
        if (startHasNotBeenReachedYet) { //meaning the 'start' character was not found after iterating through the whole string
            return "";
        }
    
        else {
            cout<< "strippedString is: " << strippedString <<endl;
            return strippedString;
        }
    
    }
    And here is the content of the file I'm passing to the program:

    enum DAY {
    saturday,
    sunday = 0,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday
    } workday;

    enum Work {
    hard = 0,
    lazy = -1
    };

    enum weirdenum //this is a weird enum...
    {

    weirdenum1,
    weirdenum2 = 2,
    weirdenum3 = 0

    };
    EDIT: Oh, and my program has not got as far as outputting to file yet (even though it created the output file, like intended). But here is the command I used to run ConvertEnumToStrings:

    Code:
    ./ConvertEnumToStrings "/home/gorilla/Documents/test.cpp" "results"
    The result file was "results.h" (created in the same directory as the inputted file), but as already mentioned, it has not got as far as outputting anything to it since the segfault stops the program before it gets a chance to.
    Last edited by Programmer_P; 06-26-2010 at 02:13 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  3. #18
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Ok, now my output statements demonstrate that that if statement is being entered (even though it is clear that the return value of the containsString() function here is false, and so the if statement is not entered.
    O_o

    that [the] if statement is being entered
    even though
    the if statement is not entered
    o_O

    Would you like to revise the following statements?

    I'm still not confused...
    I am both a musician and a programmer.
    I'm curious. Do you think that the compiler is just being unkind?

    Soma

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    O_o





    o_O

    Would you like to revise the following statements?
    Damnit! I knew someone was going to reply to that. I already edited and fixed that remark to what I really intended before you posted.

    I'm curious. Do you think that the compiler is just being unkind?

    Soma
    Apparently...
    Because the computer IS entering a if(false) statement that it shouldn't be.
    I don't know...I seem to have the most unique problems around here.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #20
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Arrogant, inexperienced, and confused?

    Are you the unholy newbie of legend?

    Soma

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    O_o

    Arrogant, inexperienced, and confused?

    Are you the unholy newbie of legend?

    Soma
    hahaha! That's a good one.
    For the record though, I'm not arrogant or confused. Inexperienced, yes, but not arrogant or confused (at least not about the topic at hand).
    And you annoying little prick...my whole life I've had a very low self-esteem. Perhaps you should look in the mirror for a glance at those same attributes you mentioned?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your main difficulty with finding your bug is that you're quite literally writing more than ten times more code than you should be!
    The following replaces 48 lines of your code (Lines 91 to 137):
    Code:
    char c = sourceString[i];
    if ('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z')
        return true;
    if (i == sourceStringSize - 1)
        return false;
    Now consider that those same lines of code appear three times and that the five lines I just showed should appear just once, in a function of its own, and there's a 25 - 30x reduction in number of lines of code.

    Lines 9 to 17 could be replaced by two strlen's.

    Lines 74 to 83 could be replaced with the one-liner:
    Code:
    return string(sourceString).find(searchString) != string::npos;
    The whole thing could probably be about 30 lines long rather than 224 lines long.

    Stuff like this is precisely why you are having problems.
    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"

  8. #23
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    That's a good one.
    Thank you; it is very kind of you to notice.

    annoying little prick
    That's "annoying medium prick" to you! ^_^v

    my whole life I've had a very low self-esteem
    Right...

    Perhaps you should look in the mirror for a glance at those same attributes you mentioned?
    I'd never forget my numerous faults. I value them in a way. Just in case I'm tempted to forget, I keep a framed list above my desk.

    Soma

  9. #24
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    That's "annoying medium prick" to you! ^_^v
    Whatever works for you....

    Right...
    Hey, its true! When I was growing up, I was very shy and didn't talk much.
    I didn't think that much of myself then (and still don't). And it always ........es me off every time I screw up too (which is often). I have never had that high and lofty thoughts about myself.

    I'd never forget my numerous faults. I value them in a way. Just in case I'm tempted to forget, I keep a framed list above my desk.

    Soma
    Lol...
    Good idea. Maybe I'll do that too.

    @iMalc: Thanks. I incorporated a lot of your advice in my new code. But it still seg-faults...
    Last edited by Programmer_P; 06-26-2010 at 03:46 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code? Instead of using debugging output statements, use a debugger.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    What is your current code? Instead of using debugging output statements, use a debugger.
    C++ code - 279 lines - codepad

    And I fixed the seg-fault. It turned out to be stripString() indeed. Apparently, it was seg-faulting because it was returning a reference to a local variable. Once I changed the function to return strippedString by value instead, the seg-fault went away. But still, the if statement is being entered when the return value of containsString is returning false, so its messing up the entire results which depend on that.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Eh, do you know that you are unnecessarily verbose in several places? For example:
    Code:
    if (searchStringIsSpecialString(searchString)) {
        //search for a character in sourceString in the range of A-Z, or a-z:
        bool trueOrFalse = sourceStringContainsAlphabetChar(sourceString);
        if (trueOrFalse == true) {
            return true;
        }
    
        else if (trueOrFalse == false) {
            return false;
        }
    }
    The above code snippet can be trivially simplified to:
    Code:
    if (searchStringIsSpecialString(searchString)) {
        //search for a character in sourceString in the range of A-Z, or a-z:
        return sourceStringContainsAlphabetChar(sourceString);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Eh, do you know that you are unnecessarily verbose in several places? For example:
    Code:
    if (searchStringIsSpecialString(searchString)) {
        //search for a character in sourceString in the range of A-Z, or a-z:
        bool trueOrFalse = sourceStringContainsAlphabetChar(sourceString);
        if (trueOrFalse == true) {
            return true;
        }
    
        else if (trueOrFalse == false) {
            return false;
        }
    }
    The above code snippet can be trivially simplified to:
    Code:
    if (searchStringIsSpecialString(searchString)) {
        //search for a character in sourceString in the range of A-Z, or a-z:
        return sourceStringContainsAlphabetChar(sourceString);
    }
    Yes, I know, but I figure my method is more clearer when you're looking at the code.
    Anyway, none of this has anything to do with a if(false) statement being entered. So far, no one has commented on this...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Programmer_P
    Yes, I know, but I figure my method is more clearer when you're looking at the code.
    No, it isn't. It makes one think what your code is actually trying to do, whereas an immediate return is straightforward.

    Quote Originally Posted by Programmer_P
    Anyway, none of this has anything to do with a if(false) statement being entered. So far, no one has commented on this...
    Simplify your code first. I do not have a compiler with me anyway, so I am literally eyeballing your code only, so please be kind and make it easier to follow.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    No, it isn't. It makes one think what your code is actually trying to do, whereas an immediate return is straightforward.
    That is a matter of opinion, but I changed the code:
    http://codepad.org/VoH75k1Z
    Last edited by Programmer_P; 06-26-2010 at 05:01 PM.
    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