0

I was solving a competitive programming question on codeforces. My code got accepted there but it is giving segmentation fault in my local computer. Why is it so?
I also tried other online compilers like ideone , and it was working there too.

My operating system is Ubuntu 20.04

My code :

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

int M = 1000000007;

int val[1001][1001];
int n,k;
int dp(int cur,int rem)
{
    if(cur<1 || cur>k || rem<0 || rem>n)return 0;
    if(cur==1 || rem==0)return 1;

    if(val[cur][rem]==-1)
    {
        int ans=0;
        ans+=dp(cur,rem-1);
        ans%=M;
        ans+=dp(cur-1,n-rem);
        ans%=M;
        val[cur][rem]=ans;
    }

    return val[cur][rem];
    
}

void solve()
{
    cin>>n>>k;

    for(int i=0;i<=k;i++)for(int j=0;j<=n;j++)val[i][j]=-1;

    cout<<dp(k,n);
    cout<<"\n";
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int _t=1;
    cin>>_t;
    for (int i=1;i<=_t;i++)
    {
        solve();
    }
    return 0;
}
Aryan Agarwal
  • 23
  • 2
  • 5
  • ideone link https://ideone.com/Bh0vGe – Aryan Agarwal May 06 '21 at 09:59
  • 4
    You should [edit] your question to contain a [mcve]. Links to volatile resources on some external site are not a suitable replacement. – Ulrich Eckhardt May 06 '21 at 10:02
  • 6
    Why on earth would you `#define int long long` or `#define F([...]) for(int i[...])`? – eike May 06 '21 at 10:04
  • @eike it was just to save time. removing them again results in segfault. – Aryan Agarwal May 06 '21 at 10:06
  • 2
    Option 1: Use valgrind. Option 2: Use sanitizers. Option 3: Enable core dumps on your system, compile your program with debug symbols, run it and analyze the core dump: https://jvns.ca/blog/2018/04/28/debugging-a-segfault-on-linux/ –  May 06 '21 at 10:09
  • 2
    Running your code under a debugger should give you some heavy hints as to where the problem lies. – G.M. May 06 '21 at 10:10
  • 4
    @AryanAgarwal If you want to save time declaring `long long int`s, just use integer literals and `auto`: `auto i = 0LL;`. Still might be considered hard to read, but at least you don't make it completely unreadable. – eike May 06 '21 at 10:10
  • 5
    I like the fact that competitive programmers write their code the way they do. It's a very fast way to filter out candidates in a job interview. If the code starts with `#include ` the job interview is usually over. –  May 06 '21 at 10:14
  • 1
    What input do you give the program to get a segmentation violation? Hardcode those values into the program in your question. – Ted Lyngmo May 06 '21 at 10:17
  • 1
    On the _time saving_ topic: Why `signed main()`? – Ted Lyngmo May 06 '21 at 10:18
  • 2
    the time you save by using such macros is payed back double, triple and more later when others read the code, same goes for "saving time" by using single letter variable names btw – 463035818_is_not_an_ai May 06 '21 at 10:50

1 Answers1

0

Turns out, my stack size was less. I used this stackoverflow answer to modify my code. Here is the correct code :

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

int M = 1000000007;

int val[1001][1001];
int n,k;
int dp(int cur,int rem)
{
    if(cur<1 || cur>k || rem<0 || rem>n)return 0;
    if(cur==1 || rem==0)return 1;

    if(val[cur][rem]==-1)
    {
        int ans=0;
        ans+=dp(cur,rem-1);
        ans%=M;
        ans+=dp(cur-1,n-rem);
        ans%=M;
        val[cur][rem]=ans;
    }

    return val[cur][rem];
    
}

void solve()
{
    cin>>n>>k;

    for(int i=0;i<=k;i++)for(int j=0;j<=n;j++)val[i][j]=-1;

    cout<<dp(k,n);
    cout<<"\n";
}

signed main()
{

    const rlim_t kStackSize = 64L * 1024L * 1024L;   // min stack size = 64 Mb
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
        if (rl.rlim_cur < kStackSize)
        {
            rl.rlim_cur = kStackSize;
            result = setrlimit(RLIMIT_STACK, &rl);
            if (result != 0)
            {
                fprintf(stderr, "setrlimit returned result = %d\n", result);
            }
        }
    }

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int _t=1;
    cin>>_t;
    for (int i=1;i<=_t;i++)
    {
        solve();
    }
    return 0;
}
Aryan Agarwal
  • 23
  • 2
  • 5