Is it stack overflow?

For input 43182 it prints 43182 to 1 which is the expected output, if we increment it by 1 means for 43183 it prints 43183 to 2 .

Seems like it is a stackoverflow. then, why i am not getting stackoverflow error?

code

#include<iostream>
using namespace std;

void get(int n)
{

  if(n==0)
  return;  

  cout<<n<<endl;

  get(n-1); 
 
}
 
int main()
{

  int n;
  
  cin>>n;

  get(n);

  return 0;
}

It is printing the integers from 43183 to 1. I don’t see any flaw there. Where are you running this code?

In sublime . Where are you running it?? maybe increase the input then you get it .

on codechef compiler i get this

suman@skynet:~$ cat > stupid.cpp
#include<iostream>
using namespace std;

void get(int n)
{

  if(n==0)
  return;  

  cout<<n<<endl;

  get(n-1); 
 
}
 
int main()
{

  int n;
  
  cin>>n;

  get(n);

  return 0;
}
^C
suman@skynet:~$ g++ -o stupid stupid.cpp -O2
suman@skynet:~$ ./stupid > output.txt
1000000
suman@skynet:~$ tail -10 output.txt
10
9
8
7
6
5
4
3
2
1
suman@skynet:~$ head -10 output.txt
1000000
999999
999998
999997
999996
999995
999994
999993
999992
999991
suman@skynet:~$ 

I executed your program on my machine. It doesn’t result in stack overflow error. As you can see, the last 10 lines and first 10 lines are as expected.

That’s because the output is huge, it’s truncated automatically by the Javascript running on your browser. You might want to check it on another website.

You can use OnlineGDB (only for testing purposes, cannot guarantee privacy during contests) for testing such things. If you want to check if it is actually printing or not, you can write the contents to a file and read the last K lines or first K lines using tail and head commands.

1 Like