jeudi 24 octobre 2019

Reason for a Segmentation Violation Error

I have a maze of characters and i want to store it into a 2d array. I have my code as nested for loop for it. However, when I test for it, I get a segmentation violation Error.

I really do not understand how is my loop going out of bounds.

MazeSolver::MazeSolver(char** m, const std::size_t r, const std::size_t c) throw(std::invalid_argument) {
    rows = r;
    cols = c;
    for (int i=0; i<rows; i++){
        for (int j=0; j<cols; j++){
           try {
                if (m[i][j] == '#' || m[i][j] == ' ' || m[i][j] == 'S' || m[i][j] == 'E') {
                        maze[i][j] = m[i][j];
                        std::cout << maze[i][j];
                        if ( maze[i][j] == 'S') {
                            row_s = i;
                            col_s = j;
                        }
                        else if( maze[i][j] == 'E') {
                            row_g = i;
                            col_g = j;
                        }
                    }
                else {
                    throw std::invalid_argument(" Maze is invalid ! Contains Invalid characters !");
                }
            }
            catch (std::invalid_argument & ex) {
                std::cout << ex.what() << std::endl;
                abort();
            } std::cout << std::endl; 
        }
    }   
}

FAILED: due to a fatal error condition: SIGSEGV - Segmentation violation signal

Aucun commentaire:

Enregistrer un commentaire