vendredi 9 août 2019

Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) in Xcode Error

I am writing some code that solves a crossword puzzle in the form of a text file. Parsing the file and creating the crossword of type vector<vector<char>> works correctly. I am encountering a EXC_BAD_ACCESS (code=EXC_I386_GPFLT) error in the horizontal_forward_search function, upon indexing the crossword, crossword[row_num][col_num].

pair<int, int> iterate(vector<vector<char>> crossword, string word) {
    int row_num = 0;

    for (vector<char> row: crossword) {
        ++row_num;
        int col_num = 0;
        for (char letter: row) {
            ++col_num;
            if (letter == word[0]) {  // first letter of word found, confirm/deny word
                pair<int, int> location = horizontal_forward_search(crossword, row_num, col_num, word);
                if (location.first != -1)
                    return location;

            }
        }
    }
    return make_pair(-1, -1);
}

and

pair<int, int> horizontal_forward_search(vector<vector<char>> crossword, int row_num, int col_num, string word) {
    long remaining_chars = crossword[row_num].size() - col_num;

    if (word.size() > remaining_chars)   // word size exceeds remaining characters
        return make_pair(-1, -1);

    else {
        string temp_str = "";
        int cur_pos = col_num;
        int i = 0;
        while (i < word.size()) {
            char cur_letter = crossword[row_num][col_num];  // Error here
            temp_str.push_back(cur_letter);
            ++cur_pos;  // next pos in array
            ++i;
        }
        if (temp_str == word)
            return make_pair(row_num, col_num);
    }
    return make_pair(-1,-1);

}

I know there are several ways to debug this instrumentally, but the code is straight forward. In the iterate function, crossword is copied over to crossword within the horizontal_forward_search function, ideally giving access to the crossword. Yet, upon doing crossword[row_num][col_num], I am met with an error.

Aucun commentaire:

Enregistrer un commentaire