-3

If you want to call a member function in a recursive manner, which one is preferred?

Case 1) instantiate the 'C', 'a' times.

void foo (int a) {
 if (a==0) return;
 Car C;
 C.memberfunc(a);
 ...
 foo(a-1);
}

Case 2) instantiate the 'C' once, and uses its member function in the recursive foo

Car C;
void foo (int a) {
 if (a==0) return;
 C.memberfunc(a);
 ...
 foo(a-1);
}

UPDATE: There is no state here. So, according to Difference between declaring variables before or in loop? Case 1) should be better. But the answers imply that Case 2) is the better option.

Can you tell me which approach is better and why? Thanks!

Community
  • 1
  • 1
Computer_guy
  • 807
  • 2
  • 11
  • 19
  • I vote for the second. Creates less stack load. – 101010 Aug 11 '14 at 23:32
  • "Better" by which criteria? – Lightness Races in Orbit Aug 11 '14 at 23:33
  • 3
    That rather depends on what `Car` is, and what `memberfunc` does. The two pieces of code are not equivalent if `Car` carries state. And if it doesn't carry state, then creating and destroying it is a no-op (and `memberfunc` should probably be static in any case). – Igor Tandetnik Aug 11 '14 at 23:33
  • Thanks for the answers. So, if there is no state, they are equivalent? How come they are no-op? the memberfunc is not static – Computer_guy Aug 11 '14 at 23:38
  • If `Car` carries no state, it will most likely not have user-defined ctor and/or dtor (nor will possible base-classes). Trivial ctor and dtor don't do anything though, thus are no-ops. Anyway, why don't you consider having a gateway function creating the `C`, which calls the recursive (inline static) implementation-function with the original parameter and a reference to that `C`? – Deduplicator Aug 11 '14 at 23:48
  • Please indent your code. – Gio Aug 11 '14 at 23:55
  • You can improve code - there is no need for recursion; this saves stack space: void foo (int a) { Car C; while (a) { int b = C.memberfunc(a); ... a--; } } – Rimas Aug 12 '14 at 00:10
  • Thanks @Deduplicator, I just wanted to know what is the overhead of doing this. But there should be some overhead of having an object. right? in the memory. I don't know why people give negative points to this! – Computer_guy Aug 12 '14 at 00:12
  • @rims: you are right. this is just an example of what I had in mind. whether to instantiate inside a function or outside of it – Computer_guy Aug 12 '14 at 00:15
  • 1
    @Computer_guy: If the compiler can elide the object ("as-if rule"), there will obviously not be any overhead for it. – Deduplicator Aug 12 '14 at 00:22

1 Answers1

2

Firstly, the question is whether both approaches are appropriate. Are they? If object C maintains a state that can to be different from one level of recursion to another then only the first approach is available to you. In the second approach, each nested recursive call will destroy the state created in C by the previous call, which is, of course, unacceptable. For example, forward pass of the recursion might store same data in C and expect that data to remain available and unchanged at the same level of recursion during the backtracking stage.

Secondly, if both approaches are indeed available to you, i.e. if C does not have any state that is specific to each level of recursion, then, of course, the second approach is better. However, using a global object is still not a good idea. The proper implementation of the second approach would create C before (and outside) the recursion and made sure all nested recursive calls have access to the same C object. Object C can still be a local object owned by the calling code. A reference to it is simply passed to the recursive calls through function arguments.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Thanks for the complete answer! I just wonder why it is different from this example http://stackoverflow.com/questions/407255/ The compiler should be smart enough! and according to the answer of that post, case 1) should be better! – Computer_guy Aug 12 '14 at 11:19