0

Will a program crash if there are so many recursive calls happen in the function?

For example:

count_number(test_number){
    if(test_number == 0)
        return 0;
    else{
        int a = "Any Random number";
        return (a + count_number(test_number - 1))/test_number;
}

Is it possible for the program to crash if I call count_number(1000000)?

I currently wrote a similar function, but when I tried to test it with 100k number of tests, the function just crashes before it even started.

Oliver Du
  • 51
  • 5
  • 5
    Most compilers use the stack when it calls a function, even a recursive call. So each call adds a few bytes to the stack. And the stack is a limited resource, on Windows the default stack size of a process is one MB. When that runs out you have *stack overflow*. However, if the program crashes *before* you call the function, or as you are starting the program, then the problem is probably something else. Use a debugger to locate the crash in your program. – Some programmer dude Nov 17 '16 at 10:06
  • I was thinking about if my function has problem too, but when I tried 3k test_number, the output was correct:/ – Oliver Du Nov 17 '16 at 10:09
  • If you show us that "similar function" then maybe we can tell you what's wrong with it. – Jabberwocky Nov 17 '16 at 10:18

0 Answers0