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?