Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should one avoid using recursive call in C/C++?

Tags:

c++

c

recursion

Should one avoid using recursive call of functions in C/C++?

I work on machine learning/data mining, so it is very critical for me to make my code scalable.

When I was using Java, I avoided using recursive call as much as possible because I often got my call stack overflowed. Although there are options to control the amount of memory assigned to the call stack, I thought having my program dependent on smaller number of parameters is more desirable. Therefore when it is clear how to implement without recursive call, possibly using a stack managed by myself, I did so. But I am not sure this is a right discipline even in Java.

In my knowledge, there is no call stack in C/C++, so I would not worry about overflowing it. Thus, I am curious: would one try to avoid using recursion, or is it encouraged, or it is problem specific, in terms of scalability of your program?

like image 768
d_ijk_stra Avatar asked Aug 03 '11 16:08

d_ijk_stra


People also ask

Should I avoid using recursion?

So even though recursion represented the algorithm in a natural way, it is very inefficient in this case. Thus, recursion may cause memory overflow if your stack space is large, and is also inefficient in cases where the same value is calculated again and again.

Is recursion safe in C?

Recursion refers to the processes of repetition of a particular task in order to achieve a specific purpose. Therefore, it is safe to say that a recursive function in C/C++ allows the programmer to call the same function within itself.

Is it better to use loops or recursion?

Recursion has more expressive power than iterative looping constructs. I say this because a while loop is equivalent to a tail recursive function and recursive functions need not be tail recursive. Powerful constructs are usually a bad thing because they allow you to do things that are difficult to read.


2 Answers

There is no single correct answer to this question. For some problems, recursion works very well. For other, it doesn't.

In my knowledge, there is no call stack in C/C++

Just to be clear, this is incorrect: there is a call stack in all implementations of C and C++ that I know of.

like image 105
NPE Avatar answered Sep 28 '22 06:09

NPE


In my knowledge, there is no call stack in C/C++, so I would not worry about overflowing it

Huh? Of course the standard doesn't talk about call stack, but indeed there is one on most, if not all, implementations.

Now, should recursion be avoided? First of all, it is well-known that every recursive function can be rewritten iteratively (i.e. without recursion). And also, sometimes iterative solutions are faster than recursive ones. But for some tasks, for example DFS in a graph, recursion is so simple and useful that you shouldn't avoid using it unless you have a good reason not to. An iterative solution for the same DFS is almost as simple, but requires more typing...

My 2 c.

like image 25
Armen Tsirunyan Avatar answered Sep 28 '22 07:09

Armen Tsirunyan