-3

Hey i wanted to make a program to solve a maze from file and to load the file in the program i used vector<string> and it's showing SIGSEGV, Segmentation fault.

#include <bits/stdc++.h>
using namespace std;

vector<string> labyrinth;
bool visited[100][100];
void printlav(vector<string> s)
    {
        for(int i = 0; i < s.size(); i++)
    {
        for(int j = 0; j < s.at(i).size(); j ++)
        {
            cout << s[i][j];
        }
        cout << "\n";
    }
}
vector<string> solve(vector<string> s,int x,int y)
{
    printlav(s);
    if(s[x][y] == 'K')
    {
        return s;
    }
    if(s[x - 1][y] != '#' && !visited[x - 1][y])
    {
        s[x - 1][y] = '+';
        visited[x - 1][y] = 1;
        s = solve(s, x - 1, y);
    }
    if(s[x + 1][y] != '#' && !visited[x + 1][y])
    {
        s[x + 1][y] = '+';
        visited[x + 1][y] = 1;
        s = solve(s, x + 1, y);
    }
    if(s[x][y -1] != '#' && !visited[x][y - 1])
    {
        s[x][y - 1] = '+';
        visited[x][y - 1] = 1;
        s = solve(s, x, y - 1);
    }
    if(s[x][y + 1] != '#' && !visited[x][y + 1])
    {
        s[x][y + 1] = '+';
        visited[x][y + 1] = 1;
        s = solve(s, x, y + 1);
    }
}
int main()
{
    string line;
    int row = 0, rs,ks;
    ifstream file_("write_here.txt");
    if(file_.is_open())
    {
        while(getline(file_, line))
        {
            labyrinth.push_back(line);
            for(int i = 0; i < line.size(); i++)
            {
                if(line[i] == 'P')
                {
                    rs = row;
                    ks  = i;
                }
            }
            row ++;

        }
        labyrinth = solve(labyrinth, rs, ks);
        for(int i = 0; i < labyrinth.size(); i++)
        {
            for(int k = 0; k < labyrinth[i].size(); k++)
            {
                cout << labyrinth[i][k];
            }
        }
        file_.close();

    }
    else
        cout << "File isn't open";
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
luke
  • 3
  • 3
  • Debug it yourself !! – Rajeev Singh Apr 25 '17 at 18:42
  • Not looking too hard but one problem is you pass your vector by value. That can run you out of memory if the vector is large and you have a lot of recursive calls. – NathanOliver Apr 25 '17 at 18:42
  • Time to debug your program. – Lightness Races in Orbit Apr 25 '17 at 18:43
  • Probably off topic: [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Together they merge into a giant Voltron-like bug factory and engine of code-death. This is a Bad Idea. – user4581301 Apr 25 '17 at 19:22
  • Interesting fun fact: You have a compiler warning that GCC should be absolutely screaming at you to fix. I strongly recommend taking the compiler's advice before you waste much more time. – user4581301 Apr 25 '17 at 19:27

1 Answers1

0

There's lots of issues in this code, but I guess the segmentation vault comes from an array index out of bounds.

Some things to consider:

  1. Always initialize variables (what if the file doesn't contain any 'P's? ...)
  2. Always be sure (=if you arent, then check in code!) that you don't access array elements that do not exist (check if the index you wanna check is between 0 and 99 in your case)
  3. Like @NathanOliver pointed out, recursive calls with your vector by value could lead to stack overflow. But currently your algorithm depends on having a copy of the maze in your method. If this is the cause of the segmentation vault, you'd need to re-think about your algorithm.
  4. in printlav you print every single character on its own. You could print the whole line by cout << s[i] << endl;
  5. in solve I guess you should return s and think if you want if's or else if's in that function (idk about your algorithm)
  6. If after considering points 2. and 3. the segmentation vault still arises (or if you want to find out the line causing the segmentation vault), you could debug the code. But a first, quick impression could be provided by printing out some debug messages onto the console (like a message before each "if" statement in the solve method). This way you should be able to locate the cause of the segmentation vault easily.
Stefan Woehrer
  • 680
  • 4
  • 12