Should you use recursive call in C / C ++?

Should you use recursive function calls in C / C ++?

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

When I used Java, I avoided using the recursive call as much as possible because I often overloaded my call stack. Although there are options for controlling the amount of memory allocated to the call stack, I thought it was more likely that my program depended on fewer parameters. Therefore, when it becomes clear how to implement without a recursive call, perhaps using a stack managed by me, I did it. But I'm not sure if this is the right discipline even in Java.

As far as I know, there is no call stack in C / C ++, so I would not worry about its overflow. So, I am curious: will you try to avoid using recursion, or is it recommended, or is it a problem in terms of the scalability of your program?

+5
source share
5 answers

The call stack in C and C ++ is similar to vtable in C ++ in one respect - it is not mentioned in the standard, as far as I know, i.e. implementation is not provided, but for all practical purposes, it was almost always carried out in this way.

, , , . , , - . ( , ).

, ... , : ?

, , , . , .

, , .. , . , /, " ". () , , , "" . , , .

Java, , .

+3

. . .

, C/++

, , : C ++, .

+11

, C/++ ,

? , , , , .

, ? , , (.. ). , , . , DFS , , , . DFS , ...

2 c.

+7

.

- . , . :

int recurse(int maxlimit, ...)
{
   if (!--maxlimit) return false; // and throw an exception or something
   ...
   return completed ? finalvalue : recurse(maxlimit, ...);
}
+2

. C/++ .

C/++:

, .

, DFS , , O (log n). . ( O (n)).

+1

All Articles