Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ recursion exits without obvious reason

I wrote a function using a recursion. While testing it, it turned out, that the function is killed without any obvious reason, while the recursion is still running.

To test this, I wrote an infinite recursion.

On my PC this function quits after about 2 seconds and the last output is about 327400. The last number isn't always the same.

I am using Ubuntu Lucid Lynx, the GCC compiler and Eclipse as IDE. If somebody has an idea what the problem is and how I can prevent the program from exiting I would be really pleased.

#include <iostream>

void rek(double x){
    std::cout << x << std::endl;
    rek(x + 1);
}

int main(int argc, char **argv) {
    rek(1);
}
like image 543
Crisscross Avatar asked May 18 '11 14:05

Crisscross


People also ask

Will a recursive function without an end condition ever quit?

A recursive function must have at least one condition where it will stop calling itself, or the function will call itself indefinitely until JavaScript throws an error. The condition that stops a recursive function from calling itself is known as the base case.

Does recursion ever stop?

A recursive function always has to say when to stop repeating itself. There should always be two parts to a recursive function: the recursive case and the base case. The recursive case is when the function calls itself. The base case is when the function stops calling itself.

What happens if a recursive function never reaches a base case?

If a recursion never reaches a base case, it will go on making recursive calls forever and the program will never terminate. This is known as infinite recursion, and it is generally not considered a good idea. In most programming environments, a program with an infinite recursion will not really run forever.

What is recursion in c language?

Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.


3 Answers

You are most likely overflowing the stack, at which point your program will be summarily killed. The depth of the stack will always limit the amount you can recurse, and if you are hitting that limit, it means your algorithm needs to change.

like image 124
dlev Avatar answered Sep 24 '22 05:09

dlev


I think you are right in expecting the code to run forever, as explained in

How do I check if gcc is performing tail-recursion optimization?

your code should be able to run for ever and ever, if gcc is performing tail recursion. On my machine it looks like -O3 actually makes gcc generate tail calls and actually flatten the stack. :-)

I surgest you set the optimize flag to O2 or O3.

like image 38
Martin Kristiansen Avatar answered Sep 25 '22 05:09

Martin Kristiansen


You are causing a stack overflow (running out of stack space) because you don't provide an exit condition.

void rek(double x){
   if(x > 10)
      return;
   std::cout << x << std::endl;
   rek(x + 1);
}
like image 25
Dave Rager Avatar answered Sep 24 '22 05:09

Dave Rager