views:

304

answers:

5

I've gotten a crash in my program which seems fine to me, of course my program says otherwise, which confuses me.

This snippet of my function I am currently working on:

        for(int k = 0; k < dictionary[k].size(); k++)
        {
            //"i" represents the fragment's first character
            //while "k" represents the dictionary first character
            if(fragments[i][j] == dictionary[k][j]) {
                token++;
                cout << token << endl;
            }
        }

might be the problem. When I debug the problem, the debugger goes to the first line in the snippet:

    for(int k = 0; k < dictionary[k].size(); k++)

then crashes when I try go to the next. In the debugger, this window opens up in my Code Blocks:

Signal Received

Program Received Singal SIGEGV, segmentation fault. Do you want to view backtrace?

I clicked yes and it just seems arbitrary to me.

Does anyone know what I've done wrong?

If the backtrace is needed(the window says Call Stack), I'll edit it on later if needed

EDIT: This is the whole function, which I've thought it would not be necessary

void Language::compare()
{
    int para = getParameters(0); //eg. 3

    int valid = para;
    int token = 0;

    for(int i = 0; i < para; i++)
    {
        //If the string is creater than 2 characters
        if(fragments[i].length() > 1) {
            for(int j = 0; j < fragments[i].length(); j++)
            {
                //Checking if that character match in dictionary
                for(int k = 0; k < para; k++) //Changed and now works,
                {
                    //"i" represents the fragment's first character
                    //while "k" represents the dictionary first character
                    if(fragments[i][j] == dictionary[k][j]) { //But now this line crashes
                        token++;
                        cout << token << endl;
                    }
                }
                if(token == 0) {
                    break;
                }
            }
        }
        else {
        //...
        }
    }
}

dictionary and fragments is declared in the class "Language" which are both vector.

+7  A: 

This line:

for(int k = 0; k < dictionary[k].size(); k++)

does seem suspect. Are you sure you don't want to loop up to the size of the dictionary array/collection?

If you posted the definition of dictionary it might help us to give some concrete suggestions.

Simon Nickerson
dictionary is a vector<string> and I can see why it's a bit of a problem
+9  A: 

I doubt you intended to use dictionary[k].size() as part of your loop control there, since the loop is iterating over k. Did you mean dictionary.size() or dictionary[i].size() perhaps?

VoteyDisciple
I intent was actually dictionary.size(), thank you, but for some reason, I'm still receiving the same error.
A: 

What is the value of j on entry to the loop?

And I agree with the other guys:

for(int k = 0; k < dictionary[k].size(); k++)

seems bogus. Something like

for(int k = 0; k < dictionary[m].size(); k++)

would seem to make more sense.

John R. Strohm
A: 

The other comments about the test in the for loop looking suspect are probably correct, but in addition if you're getting this segfault the first time you try to go into the loop then it's possible that you're working with a dictionary vector<> that has no entries.

In this case dictionary[0].size() has undefined behavior (which might well be a segfault).

Michael Burr
+1  A: 

fragments[i][j] == dictionary[k][j]

You should make sure dictionary[k].size()>j before you attempt to dereference dictionary[k][j].

MSN