0

This is the logic I came up with, but I am getting SIGSEGV error. and I don't know why it is happening.

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

int path(int m, int n,int dist[1000][1000]) {
    if(dist[m][n]!=-1) {
        return dist[m][n];
    }
    else if(m==1 && n==1) {
        return 1;
    }
    else if(m==0 || n==0) {
        return 0;
    }
    dist[m][n]=path(m-1,n,dist)+path(m,n-1,dist);
    return dist[m][n];
}

int main() {
    // your code goes here
    int dist[1000][1000];
    memset(dist, -1, sizeof(dist)*1000*1000);

    int s=path(3,3,dist);
    return 0;
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
  • `sizeof(dist)` is 4 MB. Your memset is filing 4 *terabytes* with 0xff. – Botje Nov 23 '21 at 10:50
  • And once you fix that, you are very likely to run into stack size limitations. See [this question](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) or [this one](https://stackoverflow.com/questions/31352381/stack-overflow-exception-when-declaring-multidimensional-arrays) – Botje Nov 23 '21 at 10:52

0 Answers0