Recursive calls causing seg fault

Revision en1, by jiraiya_777, 2020-08-09 21:08:42

This code snippet is giving: Segmentation fault (core dumped) on my laptop, but running just fine on Custom test on codeforces when I give input like 10^6 or so.

I think the problem is related to space taken by recursive calls, but i don't understand it completely.

Help!!

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

int n;

int solve(int i){
    if(i==(n-1)){
        return 1;
    }
    int sum1 = solve(i+1);    
    return 1;    
}

int main(){
    int i;
    cin>>n;
    int ans =solve(1);
    cout<<ans;
    return 0;
}

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English jiraiya_777 2020-08-09 21:08:42 609 Initial revision (published)