jiraiya_777's blog

By jiraiya_777, history, 6 weeks ago, In English

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;
}
 
 
 
 
  • Vote: I like it
  • -14
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

Every platform has its own stack size limit. the limit of Codeforces is much bigger than others.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is the answer. Well, in fact, you can manually set your stack size limit when compiling, so you can either have more or less memory inside the stack.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I understand that, but I don't think these 10^6 calls would take that much storage and this has never happened with me before. Can you tell how much space are these calls taking?

    • »
      »
      »
      6 weeks ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I am not an expert on this (seems like assembly code problem) but I think every call will take memory for func parameters (in this case an integer) and probably the return address.