0

I wrote the below code and it is showing me segmentation fault but when I run in any online compiler it is working fine. Can anyone please explain to me what is the reason for this strange behaviour ?

For added information, I'm running Linux Ubuntu with GCC compiler. Moreover, i noticed that if I put a return statement after std::cin>>n, then it doesn't show the error, but as soon as the input loop starts where I take the input for maze[i][j], it shows error. And I'm not going out of bound for the array, I am taking n as 5.

#include<iostream>
#define ll long long int

bool safe(ll x, ll y, ll n, ll value)
{
    if(x>=0 and x<n and y>=0 and y<n and value==1)
        return true;
    return false;
}

bool solve_maze(ll maze[1005][1005], ll sol[1005][1005], ll n, ll x , ll y)
{
    if(x==n-1 and y==n-1)
    {
        sol[x][y]=1;
        return true;
    }
    if(safe(x,y,n,maze[x][y]))
    {
        sol[x][y]=1;
        if(solve_maze(maze,sol,n,x+1,y))
            return true;
        if(solve_maze(maze,sol,n,x,y+1))
            return true;
        sol[x][y]=0;
        return false;
    }
    return false;
}


void solve()
{
    ll maze[1005][1005];
    ll sol[1005][1005];

    ll n;
    std::cin>>n; // input is n = 5
    for(ll i=0;i<n;++i)
    {
        for(ll j=0;j<n;++j)
        {
            std::cin>>maze[i][j];
            sol[i][j]=0;
        }
    }
    if(solve_maze(maze,sol,n,0,0))
    {
        for(ll i=0;i<n;++i)
        {
            for(ll j=0;j<n;++j)
                std::cout<<sol[i][j]<<" ";
            std::cout<<"\n";
        }
    }
    else
        std::cout<<"No Solution Exist\n";
}

int main()
{
    solve();
}
aman kapoor
  • 122
  • 6
  • 9
    `ll maze[1005][1005];ll sol[1005][1005];` -- This more than likely blew out the stack memory. Do not declare local arrays of this size. There is `std::vector` for this purpose. Also you shouldn't use those awful macros like `ll`, making the code hard to read. – PaulMcKenzie Jun 19 '20 at 17:13
  • 7
    This reads like it's from some contest/challenge/competitive coding/hacking site. Is it? If your goal is to learn C++, you won't learn anything there. In nearly all cases, like this one, the correct solution is based on a mathematical or a programming trick. If you don't know what the trick is and attempt to code a brute-force approach, the program runs slow and fails for that reason. If you're trying to learn C++, you won't learn anything from meaningless online contest sites [but only from a good C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Jun 19 '20 at 17:14
  • @PaulMcKenzie okay, i understood what you are saying, I never thought taking two arrays with such size can actually be too much for stack. I will change the code with vectors. thank you. And yes it is a bad habit to use ll, it is just sometime convenient for competitive coding you know. Thank you – aman kapoor Jun 19 '20 at 17:17
  • clang++ -fsanitize=address -Wall -ggdb -O0 -Wextra code.cpp and run will give you the line of the overflow – OznOg Jun 19 '20 at 17:18
  • 1
    Btw, `#define ll long long int` ... not only is `ll` a bad name, don't use `define`s for this purpose. Use `typedef`s like. `using ll = long long;` – Ted Lyngmo Jun 19 '20 at 17:18
  • @TedLyngmo Doesnt arrays are passed on by reference? I didn't know recursion makes copies of arrays, Thanks for the information. And yes, I usually typedef but recently i learnt about #define and wanted to try it out and see. I know ll is a bad name and makes confusion, I will work on improving these habits. – aman kapoor Jun 19 '20 at 17:22
  • @SamVarshavchik I was learning the concept of Rat in a Maze problem and tried to construct a solution for that problem. I will surely work on improving my knowledge. Thank you – aman kapoor Jun 19 '20 at 17:23
  • @amankapoor You're correct. I didn't think the array part through :) Try to stay away from the defines as much as you can though. – Ted Lyngmo Jun 19 '20 at 17:30
  • 1
    On desktop hardware and operating systems stack space is typically between 1 and 10 MB. If you have a stack-heavy application it's best to know how much you have available. `ll maze[1005][1005];` will be a minimum of 8080200 bytes, so just one of those arrays puts you over the top for many systems. – user4581301 Jun 19 '20 at 17:30

0 Answers0